Initial addition of CONSTANT SECTION#278
Initial addition of CONSTANT SECTION#278RamyGeorge wants to merge 2 commits intoOCamlPro:gitside-gnucobol-3.xfrom
Conversation
GitMensch
left a comment
There was a problem hiding this comment.
That's a good first take!
Please add cobc/Changelog to your changes as well-
please see https://sourceforge.net/p/gnucobol/wiki/Style%20guide/ for some general hints
The test currently fail (run make check and after the first one make check TESTSUITEFLAGS=--recheck --verbose locally to see them) because of
-prog.cob:6: error: Items in CONSTANT SECTION must have VALUE
+prog.cob:2: note: free format detected
+prog.cob:7: error: Items in CONSTANT SECTION must have VALUE
+prog.cob:7: warning: handling of CONSTANT SECTION code generation is unfinished; implementation is likely to be changed
for line two: start DIVISIONs and SECTIONs and paragraphs at column 8 and each "real" code at column 12; line 7 will be line 6 after the suggested change
cobc/field.c
Outdated
| if(f->storage == CB_STORAGE_CONSTANT){ | ||
| if(!f->values){ | ||
| cb_error(_("Items in CONSTANT SECTION must have VALUE ")); |
There was a problem hiding this comment.
formatting: see whitespace as used in other places (and drop the additional trailing spaces in line 3411, please)
msgid (that's hard to get right from the start, you only can get a clue how it might look like when checking the others) should possibly be "item in CONSTANT SECTION must have a VALUE clause" (again no trailing space)
cobc/field.c
Outdated
| } | ||
|
|
||
| if(f->storage == CB_STORAGE_CONSTANT){ | ||
| if(!f->values){ |
There was a problem hiding this comment.
I'd have to check the rules but it could be that for a record structure only part of it needs a VALUE clause - I really don't know (the Fuji manual should have the rules in).
cobc/parser.y
Outdated
| COBC_HD_SCREEN_SECTION = (1U << 17), | ||
| COBC_HD_PROCEDURE_DIVISION = (1U << 18) | ||
| COBC_HD_PROCEDURE_DIVISION = (1U << 18), | ||
| COBC_HD_CONSTANT_SECTION = (1U << 19) |
There was a problem hiding this comment.
this should have the right order to be able to use > / >= operators;
per Fuji manual CONSTANT SECTION goes before LINKAGE (As you did with the parser later on
cobc/parser.y
Outdated
|
|
||
| | constant_section SECTION _dot | ||
| { | ||
| check_headers_present(COBC_HD_DATA_DIVISION,0,0,0); // check for Data division before hand |
There was a problem hiding this comment.
the comment is correct but superfluous here, note that per style guide: we only use C89 comment blocks (as GC still compiles on C89 compilers that don't have // inline commments)
cobc/field.c
Outdated
|
|
||
| if(f->storage == CB_STORAGE_CONSTANT){ | ||
| if(!f->values){ | ||
| cb_error(_("Items in CONSTANT SECTION must have VALUE ")); |
There was a problem hiding this comment.
you want cb_error_x (CB_TREE(f), .... here as the current line number is already after the field and you want its starting position
cobc/field.c
Outdated
| cb_error(_("Items in CONSTANT SECTION must have VALUE ")); | ||
| } | ||
|
|
||
| CB_UNFINISHED ("CONSTANT SECTION code generation"); |
There was a problem hiding this comment.
that would come for every line (please add a second var, maybe a record without VALUE to the test to highlight it) - most easy solution would possibly be to add it on the program validation level if constant_section is non-NULL
There was a problem hiding this comment.
The only place I did find such validation is cb_validate_program_data. I am not exactly sure where to put it within all the logic but i have added a check for cb_storage_constant at the end and called for CB_UNFINISHED)
There was a problem hiding this comment.
yes similar, but instead of the constant you'd check the program's ->constant_storage (once it gets any addition, I don't think the constant section elements currently do get linked there, but into the previous storage).
cobc/parser.y
Outdated
| cb_tree x; | ||
| const int level = cb_get_level ($1); | ||
|
|
||
There was a problem hiding this comment.
trim trailing whitespace
| struct cb_field *linkage_storage; /* LINKAGE */ | ||
| struct cb_field *screen_storage; /* SCREEN */ | ||
| struct cb_field *report_storage; /* REPORT */ | ||
| struct cb_field *constant_storage; /* CONSTANT */ |
There was a problem hiding this comment.
that is the right thing to add - but you'd have to add the fields to that as well and then "read" the fields from there later on (have a look how that is done with working_storage)
There was a problem hiding this comment.
This I'm not exactly sure what should be added. I thought this is mainly for building the AST so I'm not particularly sure what is missing
There was a problem hiding this comment.
When producing AST we have a "current storage" where fields are added to (a linked list of 01/77 fields) - I think while the current_storage is setup correctly, the assignment to add fields to that list isn't.
For other storage elements those are already used for the symbol printing (have a look at code for -tsymbols - it actually should be added there) and at the end for the codegen.
The new storage will also be added (but just for the -fdump part) into codegen by starting from that linked list.
When you add the new storage to those two places, you'll likely see that they are "empty".
|
|
||
| AT_CHECK([$COMPILE_ONLY prog.cob], [1], [],[prog.cob:6: error: Items in CONSTANT SECTION must have VALUE | ||
| ]) | ||
| AT_CLEANUP No newline at end of file |
There was a problem hiding this comment.
that wants a line break at the end; please also have a look at how the other tests are formatted (only slightly different)
tests/testsuite.src/syn_move.at
Outdated
| AT_SETUP([MOVE to CONSTANT SECTION item]) | ||
| AT_KEYWORDS([extension constant move]) | ||
|
|
||
| AT_XFAIL_IF([true]) |
There was a problem hiding this comment.
add a comment why that is an expected fail ("to be implemented soon" :-)
each new AT_SETUP gets two empty lines above, so please drop one
|
follow-up additions after the mentioned issues:
|
GitMensch
left a comment
There was a problem hiding this comment.
mostly testing issues in this review iteration;
please have a look at the already specified changes to -fdump and add a new test to listings.at with -ftsymbol (just edit an existing test and add a CONSTANT SECTION into it, then see if the symbol listing includes those or not
| @@ -1,3 +1,11 @@ | |||
| 2026-03-03 Ramy George <ramygeorge19@gmail.com> | |||
There was a problem hiding this comment.
please start with a newline (that helps with a bunch of diff algrothms for some reason)
| CONSTANT SECTION. | ||
| 01 NUM-CONST PIC 9(3). | ||
| 01 OTHER-CONST PIC 9(3) VALUE 123. | ||
| PROCEDURE DIVISION. |
There was a problem hiding this comment.
please add a third constant with a constant record
| STOP RUN. | ||
| ]) | ||
|
|
||
| AT_CHECK([$COMPILE prog.cob], [1], [],[ignore]) |
There was a problem hiding this comment.
you may add -Wno-unfinished to the command line - please add the expected error into stderr instead of ignore (see tests above how to split the AT_CHECK into multiple lines for that
| AT_SETUP([MOVE CONSTANT SECTION to field]) | ||
| AT_KEYWORDS([extension constant move]) | ||
|
|
||
| AT_XFAIL_IF([true]) | ||
|
|
||
| AT_DATA([prog.cob],[ | ||
| IDENTIFICATION DIVISION. | ||
| PROGRAM-ID. prog. | ||
| DATA DIVISION. | ||
| CONSTANT SECTION. | ||
| 01 MY-CONST PIC 9(3) VALUE 123. | ||
| WORKING-STORAGE SECTION. | ||
| 01 RESULT-FLD PIC 9(3). | ||
| PROCEDURE DIVISION. | ||
| MOVE MY-CONST TO RESULT-FLD. | ||
| DISPLAY RESULT-FLD. | ||
| STOP RUN. | ||
| ]) | ||
| # This is also not implemented yet but should help towards the bigger issue with CONSTANTs being replaced by literals | ||
| AT_CHECK([$COMPILE prog.cob], [0], [], []) | ||
| AT_CLEANUP |
There was a problem hiding this comment.
this test should be moved to run_extensions (with -Wno-unfinished in the compilation) and should include testing the actual result (which I guess will be fine);
please add another runtime test into run_extensions.at that has an alphanumeric constant and uses refererence modification [substring], like MOVE MY-CONST (2:2) TO RESULT-FLD. - I guess that will currently fail during compile.
| switch (storage) { | ||
| case CB_STORAGE_CONSTANT: | ||
| return "Constants"; | ||
| return "CONSTANT SECTION"; |
There was a problem hiding this comment.
Constants may should be lower-cased (not sure), but CONSTANT SECTION is wrong here as other constants have the same storage but are not "living" in that section
Summary
This pull request introduces the CONSTANT SECTION as defined for Fujitsu Language reference. Its intended purpose is to introduce VALUES that do not change during program execution. For now, It expects record description entries.
Changes
Motivation
Specific section for adding CONSTANT values while enforcing proper syntax
Tests
Test for the grammar is added at tests/testsuite.src/syn_definition.at
Test for MOVE with a CONSTANT is expected to fail for now
Please give any feedback and/or any changes required to fulfill current PR goal.