Skip to content

Commit

Permalink
add --output-tree=FILE option to dump the AST to a file
Browse files Browse the repository at this point in the history
May be useful for debugging code generation.

Use the extension of FILE to choose the format of output:
* .ml : OCaml code format
* any other: JSON format (output validated by jsonlint-php)
  • Loading branch information
lefessan committed Jul 15, 2023
1 parent 6b44051 commit 4f413a2
Show file tree
Hide file tree
Showing 7 changed files with 2,164 additions and 2 deletions.
10 changes: 10 additions & 0 deletions cobc/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@

2023-07-15 Fabrice Le Fessant <[email protected]>

* cobc.c, output_tree.c: new options to dump the AST in text format:
--output-tree=<file>, and --output-tree-flags=<flags>. Format is
either OCaml (for files with .ml extension) or JSON. Flags are
'+/-' for enable/disable, 'c' for cb_common, 'l' for locations,
't' for types, 'p' for pointers, 'i' for indentation, 'n' for
newlines, 'A' for all infos. COB_OUTPUT_TREE_FLAGS env variable
can also be used to set the flags.

2023-07-11 Fabrice Le Fessant <[email protected]>

* parser.y: fix code generation for OPEN/CLOSE with multiple
Expand Down
2 changes: 1 addition & 1 deletion cobc/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ bin_PROGRAMS = cobc
cobc_SOURCES = cobc.c cobc.h ppparse.y pplex.c parser.y scanner.c config.c \
reserved.c error.c tree.c tree.h field.c typeck.c codegen.c help.c \
config.def flag.def warning.def codeoptim.def ppparse.def \
codeoptim.c replace.c
codeoptim.c replace.c output_tree.c

#cobc_SOURCES = cobc.c cobc.h ppparse.y pplex.l parser.y scanner.l config.c

Expand Down
26 changes: 25 additions & 1 deletion cobc/cobc.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ enum compile_level {
#define CB_FLAG_GETOPT_EBCDIC_TABLE 14
#define CB_FLAG_GETOPT_DEFAULT_COLSEQ 15
#define CB_FLAG_MEMORY_CHECK 16

#define CB_FLAG_OUTPUT_TREE 17
#define CB_FLAG_OUTPUT_TREE_FLAGS 18

/* Info display limits */
#define CB_IMSG_SIZE 24
Expand Down Expand Up @@ -444,6 +445,8 @@ static int save_all_src = 0;
static signed int save_c_src = 0;
static signed int verbose_output = 0;
static int cb_coverage_enabled = 0;
static char* output_tree_to_file = NULL;
static char* output_tree_flags = NULL;
static int cob_optimize = 0;


Expand Down Expand Up @@ -608,6 +611,8 @@ static const struct option long_options[] = {
{"MT", CB_RQ_ARG, NULL, '!'},
{"MF", CB_RQ_ARG, NULL, '@'},
{"coverage", CB_NO_ARG, &cb_coverage_enabled, 1},
{"output-tree", CB_RQ_ARG, NULL, CB_FLAG_OUTPUT_TREE},
{"output-tree-flags", CB_RQ_ARG, NULL, CB_FLAG_OUTPUT_TREE_FLAGS},
{"P", CB_OP_ARG, NULL, 'P'},
{"Xref", CB_NO_ARG, NULL, 'X'},
{"use-extfh", CB_RQ_ARG, NULL, 9}, /* this is used by COBOL-IT; Same is -fcallfh= */
Expand Down Expand Up @@ -3638,6 +3643,22 @@ process_command_line (const int argc, char **argv)
}
break;

case CB_FLAG_OUTPUT_TREE:
if (output_tree_to_file)
cobc_main_free (output_tree_to_file);
output_tree_to_file = cobc_main_strdup (cob_optarg);
break;

case CB_FLAG_OUTPUT_TREE_FLAGS:
if (output_tree_flags){
char* old = output_tree_flags;
output_tree_flags = cobc_main_stradd_dup(output_tree_flags, cob_optarg);
cobc_main_free (old);
} else {
output_tree_flags = cobc_main_strdup (cob_optarg);
}
break;

case '@':
/* -MF <file> */
cb_depend_file = fopen (cob_optarg, "w");
Expand Down Expand Up @@ -8079,6 +8100,9 @@ process_translate (struct filename *fn)
}
}

if (output_tree_to_file)
cb_output_tree_to_file (current_program, output_tree_to_file, output_tree_flags);

/* Translate to C */
codegen (current_program, fn->translate);

Expand Down
1 change: 1 addition & 0 deletions cobc/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -13722,6 +13722,7 @@ codegen_internal (struct cb_program *prog, const int subsequent_call)
}
}

/* output the procedure division code */
output_internal_function (prog, prog->parameter_list);

if (!prog->next_program) {
Expand Down
1 change: 1 addition & 0 deletions cobc/help.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ cobc_print_usage_common_options (void)
puts (_(" -A <options> add <options> to the C compile phase"));
puts (_(" -Q <options> add <options> to the C link phase"));
puts (_(" --coverage instrument generated binaries for coverage"));
puts (_(" --output-tree=<file> output the AST to <file> for debugging"));
puts (_(" --conf=<file> user-defined dialect configuration; see -std"));
puts (_(" --list-reserved display reserved words"));
puts (_(" --list-intrinsics display intrinsic functions"));
Expand Down
Loading

0 comments on commit 4f413a2

Please sign in to comment.