Skip to content

Commit 2664dd9

Browse files
Fix the bug about EXTERNAL data item (#747)
* fix: codegen.c * Fix a bug about data items with EXTERNAL * test: EXTERNAL item tests in run/miscellaneous.at * Enable the skipped tests about EXTERNAL * fix the format in codegen.c * fix: reflect Copilot reviews * test: Add test for EXTERNAL data item * fix: remove the commented lines * fix: separate the if statement
1 parent 5f52306 commit 2664dd9

File tree

2 files changed

+100
-60
lines changed

2 files changed

+100
-60
lines changed

cobj/codegen.c

Lines changed: 31 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -853,40 +853,32 @@ static void joutput_base(struct cb_field *f) {
853853
// EDIT
854854
/* Base name */
855855
strcpy_identifier_cobol_to_java(name, top->name);
856-
if (!top->flag_external) {
857-
register_data_storage_list(f, top);
858-
}
856+
857+
register_data_storage_list(f, top);
859858

860859
if (!top->flag_base) {
861-
if (!top->flag_external) {
862-
if (!top->flag_local || top->flag_is_global) {
863-
bl = cobc_malloc(sizeof(struct base_list));
864-
bl->f = top;
865-
bl->curr_prog = excp_current_program_id;
866-
bl->next = base_cache;
867-
base_cache = bl;
860+
if (!top->flag_local || top->flag_is_global) {
861+
bl = cobc_malloc(sizeof(struct base_list));
862+
bl->f = top;
863+
bl->curr_prog = excp_current_program_id;
864+
bl->next = base_cache;
865+
base_cache = bl;
866+
} else {
867+
if (current_prog->flag_global_use) {
868+
joutput_local("unsigned char\t\t*%s%s = NULL;", CB_PREFIX_BASE, name);
869+
joutput_local("\t/* %s */\n", top->name);
870+
joutput_local("static unsigned char\t*save_%s%s;\n", CB_PREFIX_BASE,
871+
name);
868872
} else {
869-
if (current_prog->flag_global_use) {
870-
joutput_local("unsigned char\t\t*%s%s = NULL;", CB_PREFIX_BASE, name);
871-
joutput_local("unsigned char\t\t*%s%s = NULL;", CB_PREFIX_BASE, name);
872-
joutput_local("\t/* %s */\n", top->name);
873-
joutput_local("static unsigned char\t*save_%s%s;\n", CB_PREFIX_BASE,
874-
name);
875-
} else {
876-
joutput_local("unsigned char\t*%s%s = NULL;", CB_PREFIX_BASE, name);
877-
joutput_local("\t/* %s */\n", top->name);
878-
}
873+
joutput_local("unsigned char\t*%s%s = NULL;", CB_PREFIX_BASE, name);
874+
joutput_local("\t/* %s */\n", top->name);
879875
}
880876
}
881877
top->flag_base = 1;
882878
}
883879

884-
if (top->flag_external) {
885-
joutput("%s%s", CB_PREFIX_BASE, name);
886-
} else {
887-
if (joutput_field_storage(f, top) && f->offset != 0) {
888-
joutput(".getSubDataStorage(%d)", f->offset);
889-
}
880+
if (joutput_field_storage(f, top) && f->offset != 0) {
881+
joutput(".getSubDataStorage(%d)", f->offset);
890882
}
891883

892884
if (cb_field_variable_address(f)) {
@@ -2260,14 +2252,14 @@ static void joutput_initialize_external(cb_tree x, struct cb_field *f) {
22602252
joutput_prefix();
22612253
joutput_data(x);
22622254
if (f->ename) {
2263-
joutput(" = CobolExternal.getStorageAddress (\"%s\", %d);\n", f->ename,
2255+
joutput(" = CobolExternal.getStorageAddress (\"%s\", %d);", f->ename,
22642256
f->size);
22652257
} else if (f->storage == CB_STORAGE_FILE) {
22662258
file = CB_TREE(f->file);
2267-
joutput(" = CobolExternal.getStorageAddress (\"%s\", %d);\n",
2259+
joutput(" = CobolExternal.getStorageAddress (\"%s\", %d);",
22682260
CB_FILE(file)->record->name, f->size);
22692261
} else {
2270-
joutput(" = CobolExternal.getStorageAddress (\"%s\", %d);\n", f->name,
2262+
joutput(" = CobolExternal.getStorageAddress (\"%s\", %d);", f->name,
22712263
f->size);
22722264
}
22732265
}
@@ -2597,12 +2589,6 @@ static void joutput_initialize(struct cb_initialize *p) {
25972589
int c;
25982590

25992591
f = cb_field(p->var);
2600-
if (f->flag_external) {
2601-
joutput_initialize_external(p->var, f);
2602-
if (!p->flag_statement) {
2603-
return;
2604-
}
2605-
}
26062592
switch (initialize_type(p, f, 1)) {
26072593
case INITIALIZE_NONE:
26082594
break;
@@ -2622,6 +2608,7 @@ static void joutput_initialize(struct cb_initialize *p) {
26222608
break;
26232609
}
26242610
}
2611+
26252612
/*
26262613
* SEARCH
26272614
*/
@@ -4240,6 +4227,10 @@ static void joutput_initial_values(struct cb_field *p) {
42404227
if (p->flag_no_init && !p->count) {
42414228
continue;
42424229
}
4230+
/* EXTERNAL items */
4231+
if (p->flag_external) {
4232+
continue;
4233+
}
42434234
int tmp_flag = integer_reference_flag;
42444235
integer_reference_flag = 1;
42454236
joutput_stmt(cb_build_initialize(x, cb_true, NULL, def, 0),
@@ -5079,6 +5070,11 @@ static void joutput_init_method(struct cb_program *prog) {
50795070
joutput_prefix();
50805071
joutput("%s = new CobolDataStorage(%d);", base_name,
50815072
blp->f->memory_size);
5073+
}
5074+
5075+
if (blp->f->flag_external) {
5076+
joutput_initialize_external(cb_build_field_reference(blp->f, NULL),
5077+
blp->f);
50825078
} else {
50835079
joutput_prefix();
50845080
joutput("%s = new CobolDataStorage(%d);", base_name,
@@ -5594,27 +5590,6 @@ static void joutput_declare_member_variables(struct cb_program *prog,
55945590
joutput("\n");
55955591
}
55965592

5597-
/* External items */
5598-
for (f = prog->working_storage; f; f = f->sister) {
5599-
if (f->flag_external) {
5600-
joutput_prefix();
5601-
joutput("private CobolDataStorage ");
5602-
joutput_base(f);
5603-
joutput(" = null; /* %s */", f->name);
5604-
joutput_newline();
5605-
}
5606-
}
5607-
for (l = prog->file_list; l; l = CB_CHAIN(l)) {
5608-
f = CB_FILE(CB_VALUE(l))->record;
5609-
if (f->flag_external) {
5610-
joutput_prefix();
5611-
joutput("private CobolDataStorage ");
5612-
joutput_base(f);
5613-
joutput(" = null; /* %s */", f->name);
5614-
joutput_newline();
5615-
}
5616-
}
5617-
56185593
/* AbstractCobolField型変数の宣言(非定数) */
56195594
if (field_cache) {
56205595
joutput_line("/* Fields */\n");

tests/run.src/miscellaneous.at

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ AT_CLEANUP
8888

8989

9090
AT_SETUP([EXTERNAL data item])
91-
AT_CHECK([${SKIP_TEST}])
9291

9392
AT_DATA([callee.cob], [
9493
IDENTIFICATION DIVISION.
@@ -122,7 +121,7 @@ AT_DATA([caller.cob], [
122121

123122
AT_CHECK([${COMPILE_MODULE} callee.cob])
124123
AT_CHECK([${COMPILE} caller.cob])
125-
AT_CHECK([java prog], [0],
124+
AT_CHECK([${RUN_MODULE} caller], [0],
126125
[Hello
127126
World
128127
])
@@ -131,7 +130,6 @@ AT_CLEANUP
131130

132131

133132
AT_SETUP([EXTERNAL AS data item])
134-
AT_CHECK([${SKIP_TEST}])
135133

136134
AT_DATA([callee.cob], [
137135
IDENTIFICATION DIVISION.
@@ -168,14 +166,81 @@ AT_DATA([caller.cob], [
168166

169167
AT_CHECK([${COMPILE_MODULE} callee.cob])
170168
AT_CHECK([${COMPILE} caller.cob])
171-
AT_CHECK([java prog], [0],
169+
AT_CHECK([${RUN_MODULE} caller], [0],
172170
[Extrn
173171
Hello
174172
World
175173
])
176174

177175
AT_CLEANUP
178176

177+
AT_SETUP([EXTERNAL group data item])
178+
179+
AT_DATA([prog.cbl], [
180+
IDENTIFICATION DIVISION.
181+
PROGRAM-ID. prog.
182+
DATA DIVISION.
183+
WORKING-STORAGE SECTION.
184+
01 A EXTERNAL.
185+
03 A1 PIC X(5).
186+
03 A2 PIC X(3).
187+
03 A3.
188+
05 A3-1 PIC 9(1).
189+
05 A3-2 PIC 9(1).
190+
PROCEDURE DIVISION.
191+
MOVE "HELLO" TO A1.
192+
MOVE "ABC" TO A2.
193+
MOVE 12 TO A3.
194+
195+
DISPLAY A.
196+
DISPLAY A1.
197+
DISPLAY A2.
198+
DISPLAY A3.
199+
200+
MOVE "DEF" TO A2.
201+
DISPLAY A.
202+
203+
CALL "sub".
204+
205+
MOVE 5 TO A3-2.
206+
207+
DISPLAY A.
208+
209+
STOP RUN.
210+
])
211+
212+
AT_DATA([sub.cbl], [
213+
IDENTIFICATION DIVISION.
214+
PROGRAM-ID. sub.
215+
DATA DIVISION.
216+
WORKING-STORAGE SECTION.
217+
01 A EXTERNAL.
218+
03 A1 PIC X(5).
219+
03 A2 PIC X(3).
220+
03 A3.
221+
05 A3-1 PIC 9(1).
222+
05 A3-2 PIC 9(1).
223+
PROCEDURE DIVISION.
224+
DISPLAY A.
225+
MOVE 34 TO A3.
226+
MOVE "WORLD" TO A1.
227+
GOBACK.
228+
])
229+
230+
AT_CHECK([${COBJ} prog.cbl])
231+
AT_CHECK([${COBJ} sub.cbl])
232+
AT_CHECK([java prog], [0],
233+
[HELLOABC12
234+
HELLO
235+
ABC
236+
12
237+
HELLODEF12
238+
HELLODEF12
239+
WORLDDEF35
240+
])
241+
242+
AT_CLEANUP
243+
179244

180245
AT_SETUP([java command validation])
181246

0 commit comments

Comments
 (0)