Skip to content

Commit

Permalink
add --fdump-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 d3294aa
Show file tree
Hide file tree
Showing 12 changed files with 2,290 additions and 11 deletions.
13 changes: 13 additions & 0 deletions cobc/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@

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

* cobc.c, dump_tree.c: new options to dump the AST in text format:
--dump-tree=<file>, and --dump-tree-flags=<flags>. Format is
either OCaml (for files with .ml extension) or JSON. If file
ends with '/', then it is expected to be a directory and the
file will be generated with the program id as name. 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, 'O' for OCaml format, 'J' for
JSON format. Env variables COB_DUMP_TREE and
COB_DUMP_TREE_FLAGS can also be used to set these 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 dump_tree.c

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

Expand Down
28 changes: 26 additions & 2 deletions 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_DUMP_TREE 17
#define CB_FLAG_DUMP_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* dump_tree_to_file = NULL;
static char* dump_tree_flags = NULL;
static int cob_optimize = 0;


Expand Down Expand Up @@ -1087,7 +1090,7 @@ cobc_main_strdup (const char *dupstr)
}

/* returns a fresh allocated copy of the concatenation from str1 + str2 */
static char *
char *
cobc_main_stradd_dup (const char *str1, const char *str2)
{
char *p;
Expand Down Expand Up @@ -3638,6 +3641,22 @@ process_command_line (const int argc, char **argv)
}
break;

case CB_FLAG_DUMP_TREE:
if (dump_tree_to_file)
cobc_main_free (dump_tree_to_file);
dump_tree_to_file = cobc_main_strdup (cob_optarg);
break;

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

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

if (!dump_tree_to_file)
dump_tree_to_file = getenv("COB_DUMP_TREE");
if (dump_tree_to_file)
cb_dump_tree_to_file (current_program, dump_tree_to_file, dump_tree_flags);

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

Expand Down
1 change: 1 addition & 0 deletions cobc/cobc.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ extern void *cobc_realloc (void *, const size_t);

extern void *cobc_main_malloc (const size_t);
extern void *cobc_main_strdup (const char *);
extern char *cobc_main_stradd_dup (const char *, const char*);
extern void *cobc_main_realloc (void *, const size_t);
extern void cobc_main_free (void *);

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
Loading

0 comments on commit d3294aa

Please sign in to comment.