From 5cce4232e1117a6b168ed9ad462a953b88633424 Mon Sep 17 00:00:00 2001 From: tsh-hashimoto Date: Mon, 17 Nov 2025 00:25:15 +0000 Subject: [PATCH 1/7] fix: codegen.c * Fix a bug about data items with EXTERNAL --- cobj/codegen.c | 79 +++++++++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 36 deletions(-) diff --git a/cobj/codegen.c b/cobj/codegen.c index cb220af8..f6d30168 100644 --- a/cobj/codegen.c +++ b/cobj/codegen.c @@ -853,12 +853,12 @@ static void joutput_base(struct cb_field *f) { // EDIT /* Base name */ strcpy_identifier_cobol_to_java(name, top->name); - if (!top->flag_external) { + // if (!top->flag_external) { register_data_storage_list(f, top); - } + // } if (!top->flag_base) { - if (!top->flag_external) { + // if (!top->flag_external) { if (!top->flag_local || top->flag_is_global) { bl = cobc_malloc(sizeof(struct base_list)); bl->f = top; @@ -877,17 +877,17 @@ static void joutput_base(struct cb_field *f) { joutput_local("\t/* %s */\n", top->name); } } - } + // } top->flag_base = 1; } - if (top->flag_external) { - joutput("%s%s", CB_PREFIX_BASE, name); - } else { + // if (top->flag_external) { + // joutput("%s%s", CB_PREFIX_BASE, name); + // } else { if (joutput_field_storage(f, top) && f->offset != 0) { joutput(".getSubDataStorage(%d)", f->offset); } - } + // } if (cb_field_variable_address(f)) { for (p = f->parent; p; f = f->parent, p = f->parent) { @@ -2260,14 +2260,14 @@ static void joutput_initialize_external(cb_tree x, struct cb_field *f) { joutput_prefix(); joutput_data(x); if (f->ename) { - joutput(" = CobolExternal.getStorageAddress (\"%s\", %d);\n", f->ename, + joutput(" = CobolExternal.getStorageAddress (\"%s\", %d);", f->ename, f->size); } else if (f->storage == CB_STORAGE_FILE) { file = CB_TREE(f->file); - joutput(" = CobolExternal.getStorageAddress (\"%s\", %d);\n", + joutput(" = CobolExternal.getStorageAddress (\"%s\", %d);", CB_FILE(file)->record->name, f->size); } else { - joutput(" = CobolExternal.getStorageAddress (\"%s\", %d);\n", f->name, + joutput(" = CobolExternal.getStorageAddress (\"%s\", %d);", f->name, f->size); } } @@ -2597,12 +2597,12 @@ static void joutput_initialize(struct cb_initialize *p) { int c; f = cb_field(p->var); - if (f->flag_external) { - joutput_initialize_external(p->var, f); - if (!p->flag_statement) { - return; - } - } + // if (f->flag_external) { + // joutput_initialize_external(p->var, f); + // if (!p->flag_statement) { + // return; + // } + // } switch (initialize_type(p, f, 1)) { case INITIALIZE_NONE: break; @@ -4240,6 +4240,10 @@ static void joutput_initial_values(struct cb_field *p) { if (p->flag_no_init && !p->count) { continue; } + /* EXTERNAL items */ + if (p->flag_external) { + continue; + } int tmp_flag = integer_reference_flag; integer_reference_flag = 1; joutput_stmt(cb_build_initialize(x, cb_true, NULL, def, 0), @@ -5079,6 +5083,9 @@ static void joutput_init_method(struct cb_program *prog) { joutput_prefix(); joutput("%s = new CobolDataStorage(%d);", base_name, blp->f->memory_size); + } else if (blp->f->flag_external) { + joutput_initialize_external(cb_build_field_reference(blp->f, NULL), + blp->f); } else { joutput_prefix(); joutput("%s = new CobolDataStorage(%d);", base_name, @@ -5595,25 +5602,25 @@ static void joutput_declare_member_variables(struct cb_program *prog, } /* External items */ - for (f = prog->working_storage; f; f = f->sister) { - if (f->flag_external) { - joutput_prefix(); - joutput("private CobolDataStorage "); - joutput_base(f); - joutput(" = null; /* %s */", f->name); - joutput_newline(); - } - } - for (l = prog->file_list; l; l = CB_CHAIN(l)) { - f = CB_FILE(CB_VALUE(l))->record; - if (f->flag_external) { - joutput_prefix(); - joutput("private CobolDataStorage "); - joutput_base(f); - joutput(" = null; /* %s */", f->name); - joutput_newline(); - } - } + // for (f = prog->working_storage; f; f = f->sister) { + // if (f->flag_external) { + // joutput_prefix(); + // joutput("private CobolDataStorage "); + // joutput_base(f); + // joutput(" = null; /* %s */", f->name); + // joutput_newline(); + // } + // } + // for (l = prog->file_list; l; l = CB_CHAIN(l)) { + // f = CB_FILE(CB_VALUE(l))->record; + // if (f->flag_external) { + // joutput_prefix(); + // joutput("private CobolDataStorage "); + // joutput_base(f); + // joutput(" = null; /* %s */", f->name); + // joutput_newline(); + // } + // } /* AbstractCobolField型変数の宣言(非定数) */ if (field_cache) { From 3743ffd2abc5f9b6033d15e2779f7871e1f509e7 Mon Sep 17 00:00:00 2001 From: tsh-hashimoto Date: Mon, 17 Nov 2025 00:26:11 +0000 Subject: [PATCH 2/7] test: EXTERNAL item tests in run/miscellaneous.at * Enable the skipped tests about EXTERNAL --- tests/run.src/miscellaneous.at | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/run.src/miscellaneous.at b/tests/run.src/miscellaneous.at index cc3ddfb2..6ccca231 100644 --- a/tests/run.src/miscellaneous.at +++ b/tests/run.src/miscellaneous.at @@ -88,7 +88,6 @@ AT_CLEANUP AT_SETUP([EXTERNAL data item]) -AT_CHECK([${SKIP_TEST}]) AT_DATA([callee.cob], [ IDENTIFICATION DIVISION. @@ -122,7 +121,7 @@ AT_DATA([caller.cob], [ AT_CHECK([${COMPILE_MODULE} callee.cob]) AT_CHECK([${COMPILE} caller.cob]) -AT_CHECK([java prog], [0], +AT_CHECK([${RUN_MODULE} caller], [0], [Hello World ]) @@ -131,7 +130,6 @@ AT_CLEANUP AT_SETUP([EXTERNAL AS data item]) -AT_CHECK([${SKIP_TEST}]) AT_DATA([callee.cob], [ IDENTIFICATION DIVISION. @@ -168,7 +166,7 @@ AT_DATA([caller.cob], [ AT_CHECK([${COMPILE_MODULE} callee.cob]) AT_CHECK([${COMPILE} caller.cob]) -AT_CHECK([java prog], [0], +AT_CHECK([${RUN_MODULE} caller], [0], [Extrn Hello World From e3465145b2c8deffa8b37120ca0b7857d610e413 Mon Sep 17 00:00:00 2001 From: tsh-hashimoto Date: Mon, 17 Nov 2025 00:38:35 +0000 Subject: [PATCH 3/7] fix the format in codegen.c --- cobj/codegen.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/cobj/codegen.c b/cobj/codegen.c index f6d30168..fcb0f431 100644 --- a/cobj/codegen.c +++ b/cobj/codegen.c @@ -854,29 +854,29 @@ static void joutput_base(struct cb_field *f) { /* Base name */ strcpy_identifier_cobol_to_java(name, top->name); // if (!top->flag_external) { - register_data_storage_list(f, top); + register_data_storage_list(f, top); // } if (!top->flag_base) { // if (!top->flag_external) { - if (!top->flag_local || top->flag_is_global) { - bl = cobc_malloc(sizeof(struct base_list)); - bl->f = top; - bl->curr_prog = excp_current_program_id; - bl->next = base_cache; - base_cache = bl; + if (!top->flag_local || top->flag_is_global) { + bl = cobc_malloc(sizeof(struct base_list)); + bl->f = top; + bl->curr_prog = excp_current_program_id; + bl->next = base_cache; + base_cache = bl; + } else { + if (current_prog->flag_global_use) { + joutput_local("unsigned char\t\t*%s%s = NULL;", CB_PREFIX_BASE, name); + joutput_local("unsigned char\t\t*%s%s = NULL;", CB_PREFIX_BASE, name); + joutput_local("\t/* %s */\n", top->name); + joutput_local("static unsigned char\t*save_%s%s;\n", CB_PREFIX_BASE, + name); } else { - if (current_prog->flag_global_use) { - joutput_local("unsigned char\t\t*%s%s = NULL;", CB_PREFIX_BASE, name); - joutput_local("unsigned char\t\t*%s%s = NULL;", CB_PREFIX_BASE, name); - joutput_local("\t/* %s */\n", top->name); - joutput_local("static unsigned char\t*save_%s%s;\n", CB_PREFIX_BASE, - name); - } else { - joutput_local("unsigned char\t*%s%s = NULL;", CB_PREFIX_BASE, name); - joutput_local("\t/* %s */\n", top->name); - } + joutput_local("unsigned char\t*%s%s = NULL;", CB_PREFIX_BASE, name); + joutput_local("\t/* %s */\n", top->name); } + } // } top->flag_base = 1; } @@ -884,9 +884,9 @@ static void joutput_base(struct cb_field *f) { // if (top->flag_external) { // joutput("%s%s", CB_PREFIX_BASE, name); // } else { - if (joutput_field_storage(f, top) && f->offset != 0) { - joutput(".getSubDataStorage(%d)", f->offset); - } + if (joutput_field_storage(f, top) && f->offset != 0) { + joutput(".getSubDataStorage(%d)", f->offset); + } // } if (cb_field_variable_address(f)) { @@ -5085,7 +5085,7 @@ static void joutput_init_method(struct cb_program *prog) { blp->f->memory_size); } else if (blp->f->flag_external) { joutput_initialize_external(cb_build_field_reference(blp->f, NULL), - blp->f); + blp->f); } else { joutput_prefix(); joutput("%s = new CobolDataStorage(%d);", base_name, From 2f311293cb2655c1016ecc4e49a382d55fa0f534 Mon Sep 17 00:00:00 2001 From: tsh-hashimoto Date: Mon, 17 Nov 2025 01:23:57 +0000 Subject: [PATCH 4/7] fix: reflect Copilot reviews --- cobj/codegen.c | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/cobj/codegen.c b/cobj/codegen.c index fcb0f431..abb4fe42 100644 --- a/cobj/codegen.c +++ b/cobj/codegen.c @@ -853,12 +853,10 @@ static void joutput_base(struct cb_field *f) { // EDIT /* Base name */ strcpy_identifier_cobol_to_java(name, top->name); - // if (!top->flag_external) { + register_data_storage_list(f, top); - // } if (!top->flag_base) { - // if (!top->flag_external) { if (!top->flag_local || top->flag_is_global) { bl = cobc_malloc(sizeof(struct base_list)); bl->f = top; @@ -867,7 +865,6 @@ static void joutput_base(struct cb_field *f) { base_cache = bl; } else { if (current_prog->flag_global_use) { - joutput_local("unsigned char\t\t*%s%s = NULL;", CB_PREFIX_BASE, name); joutput_local("unsigned char\t\t*%s%s = NULL;", CB_PREFIX_BASE, name); joutput_local("\t/* %s */\n", top->name); joutput_local("static unsigned char\t*save_%s%s;\n", CB_PREFIX_BASE, @@ -877,17 +874,12 @@ static void joutput_base(struct cb_field *f) { joutput_local("\t/* %s */\n", top->name); } } - // } top->flag_base = 1; } - // if (top->flag_external) { - // joutput("%s%s", CB_PREFIX_BASE, name); - // } else { if (joutput_field_storage(f, top) && f->offset != 0) { joutput(".getSubDataStorage(%d)", f->offset); } - // } if (cb_field_variable_address(f)) { for (p = f->parent; p; f = f->parent, p = f->parent) { @@ -2597,12 +2589,6 @@ static void joutput_initialize(struct cb_initialize *p) { int c; f = cb_field(p->var); - // if (f->flag_external) { - // joutput_initialize_external(p->var, f); - // if (!p->flag_statement) { - // return; - // } - // } switch (initialize_type(p, f, 1)) { case INITIALIZE_NONE: break; @@ -2622,6 +2608,7 @@ static void joutput_initialize(struct cb_initialize *p) { break; } } + /* * SEARCH */ From 24250e57fbf4037c7a6c8710d07e2449ea178efc Mon Sep 17 00:00:00 2001 From: tsh-hashimoto Date: Mon, 17 Nov 2025 03:15:12 +0000 Subject: [PATCH 5/7] test: Add test for EXTERNAL data item --- tests/run.src/miscellaneous.at | 67 ++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/tests/run.src/miscellaneous.at b/tests/run.src/miscellaneous.at index 6ccca231..4e2eee58 100644 --- a/tests/run.src/miscellaneous.at +++ b/tests/run.src/miscellaneous.at @@ -174,6 +174,73 @@ World AT_CLEANUP +AT_SETUP([EXTERNAL group data item]) + +AT_DATA([prog.cbl], [ + IDENTIFICATION DIVISION. + PROGRAM-ID. prog. + DATA DIVISION. + WORKING-STORAGE SECTION. + 01 A EXTERNAL. + 03 A1 PIC X(5). + 03 A2 PIC X(3). + 03 A3. + 05 A3-1 PIC 9(1). + 05 A3-2 PIC 9(1). + PROCEDURE DIVISION. + MOVE "HELLO" TO A1. + MOVE "ABC" TO A2. + MOVE 12 TO A3. + + DISPLAY A. + DISPLAY A1. + DISPLAY A2. + DISPLAY A3. + + MOVE "DEF" TO A2. + DISPLAY A. + + CALL "sub". + + MOVE 5 TO A3-2. + + DISPLAY A. + + STOP RUN. +]) + +AT_DATA([sub.cbl], [ + IDENTIFICATION DIVISION. + PROGRAM-ID. sub. + DATA DIVISION. + WORKING-STORAGE SECTION. + 01 A EXTERNAL. + 03 A1 PIC X(5). + 03 A2 PIC X(3). + 03 A3. + 05 A3-1 PIC 9(1). + 05 A3-2 PIC 9(1). + PROCEDURE DIVISION. + DISPLAY A. + MOVE 34 TO A3. + MOVE "WORLD" TO A1. + GOBACK. +]) + +AT_CHECK([${COBJ} prog.cbl]) +AT_CHECK([${COBJ} sub.cbl]) +AT_CHECK([java prog], [0], +[HELLOABC12 +HELLO +ABC +12 +HELLODEF12 +HELLODEF12 +WORLDDEF35 +]) + +AT_CLEANUP + AT_SETUP([java command validation]) From 8f0b5241b36d6e4fb8006faa993549410d8a821b Mon Sep 17 00:00:00 2001 From: tsh-hashimoto Date: Tue, 18 Nov 2025 05:29:16 +0000 Subject: [PATCH 6/7] fix: remove the commented lines --- cobj/codegen.c | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/cobj/codegen.c b/cobj/codegen.c index abb4fe42..dd07465f 100644 --- a/cobj/codegen.c +++ b/cobj/codegen.c @@ -5588,27 +5588,6 @@ static void joutput_declare_member_variables(struct cb_program *prog, joutput("\n"); } - /* External items */ - // for (f = prog->working_storage; f; f = f->sister) { - // if (f->flag_external) { - // joutput_prefix(); - // joutput("private CobolDataStorage "); - // joutput_base(f); - // joutput(" = null; /* %s */", f->name); - // joutput_newline(); - // } - // } - // for (l = prog->file_list; l; l = CB_CHAIN(l)) { - // f = CB_FILE(CB_VALUE(l))->record; - // if (f->flag_external) { - // joutput_prefix(); - // joutput("private CobolDataStorage "); - // joutput_base(f); - // joutput(" = null; /* %s */", f->name); - // joutput_newline(); - // } - // } - /* AbstractCobolField型変数の宣言(非定数) */ if (field_cache) { joutput_line("/* Fields */\n"); From b707d6cebf7bd2bee4ec740694ca268787e2b9d9 Mon Sep 17 00:00:00 2001 From: tsh-hashimoto Date: Tue, 18 Nov 2025 09:46:24 +0000 Subject: [PATCH 7/7] fix: separate the if statement --- cobj/codegen.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cobj/codegen.c b/cobj/codegen.c index dd07465f..ece49c9f 100644 --- a/cobj/codegen.c +++ b/cobj/codegen.c @@ -5070,7 +5070,9 @@ static void joutput_init_method(struct cb_program *prog) { joutput_prefix(); joutput("%s = new CobolDataStorage(%d);", base_name, blp->f->memory_size); - } else if (blp->f->flag_external) { + } + + if (blp->f->flag_external) { joutput_initialize_external(cb_build_field_reference(blp->f, NULL), blp->f); } else {