Skip to content

Commit

Permalink
compiler cleanup for SEARCH with improved source references and traci…
Browse files Browse the repository at this point in the history
…ng + parser cleanup

cobc:
* parser.y (table_name): check for KEY phrase in SEARCH ALL
* parser.y (end_perform_or_dot): fix terminator cleanup, previously freed the wrong cb_tree
* parser.y, scanner.l:
  * distinguish between tokens END / AT_END, ESCAPE / ON_ESCAPE, EXCEPTION / ON_EXCEPTION
  * renamed tokens NOT_END -> NOT_AT_END, NOT_ON_EXCEPTION -> NOT_EXCEPTION, NOT_OVERFLOW -> NOT_ON_OVERFLOW, NOT_ESCAPE -> NOT_ON_ESCAPE
* codegen.c (output_search_all): add WHEN tracing, to keep performance only done after the actual execution
* codegen.c (output_search_whens): add SEARCH VARYING tracing
* tree.h (struct cb_search), tree.c (cb_build_search), parser.y, typeck.c (cb_emit_search, cb_emit_search_all), codegen.c (output_search_whens, output_search_all): renamed end_stmt in cb_search to at_end, storing pair of AT END (position) and statements
* codegen.c (output_search_whens, output_search_all): adjust output of source references for better debugging experience and add AT END tracing
  • Loading branch information
sf-mensch committed Dec 30, 2021
1 parent 47a9def commit 3d5aa86
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 115 deletions.
19 changes: 19 additions & 0 deletions cobc/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@

2021-12-30 Simon Sobisch <[email protected]>

* parser.y (table_name): check for KEY phrase in SEARCH ALL
* parser.y (end_perform_or_dot): fix terminator cleanup, previously freed the
wrong cb_tree
* parser.y, scanner.l: distinguish between tokens END / AT_END,
ESCAPE / ON_ESCAPE, EXCEPTION / ON_EXCEPTION and
renamed tokens NOT_END -> NOT_AT_END, NOT_ON_EXCEPTION -> NOT_EXCEPTION,
NOT_OVERFLOW -> NOT_ON_OVERFLOW, NOT_ESCAPE -> NOT_ON_ESCAPE
* codegen.c (output_search_all): add WHEN tracing, to keep performance only
done after the actual execution
* codegen.c (output_search_whens): add SEARCH VARYING tracing
* tree.h (struct cb_search), tree.c (cb_build_search), parser.y,
typeck.c (cb_emit_search, cb_emit_search_all),
codegen.c (output_search_whens, output_search_all): renamed end_stmt in
cb_search to at_end, storing pair of AT END (position) and statements
* codegen.c (output_search_whens, output_search_all): adjust output of
source references for better debugging experience and add AT END tracing

2021-12-14 Simon Sobisch <[email protected]>

* cobc.c (print_fields), codegen.c (output_field_display): only check for
Expand Down
116 changes: 84 additions & 32 deletions cobc/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ static void output_param (cb_tree, int);
static void output_funcall (cb_tree);
static void output_report_summed_field (struct cb_field *);

static void output_trace_info (cb_tree, const char *);
static void output_source_reference (cb_tree, const char *);

static void codegen_init (struct cb_program *, const char *);
Expand Down Expand Up @@ -5423,14 +5424,12 @@ output_occurs (struct cb_field *p)
}

static void
output_search_whens (cb_tree table, struct cb_field *p, cb_tree stmt,
output_search_whens (cb_tree table, struct cb_field *p, cb_tree at_end,
cb_tree var, cb_tree whens)
{
cb_tree l;
cb_tree idx = NULL;

COB_UNUSED(table); /* to be handled later */

/* LCOV_EXCL_START */
if (!p->index_list) {
cobc_err_msg (_("call to '%s' with invalid parameter '%s'"),
Expand All @@ -5452,6 +5451,7 @@ output_search_whens (cb_tree table, struct cb_field *p, cb_tree stmt,
}

/* Start loop */
last_line = -1; /* force statement reference output at begin of loop */
output_line ("for (;;) {");
output_indent_level += 2;

Expand All @@ -5463,41 +5463,58 @@ output_search_whens (cb_tree table, struct cb_field *p, cb_tree stmt,
output_occurs (p);
output (")");
output_newline ();
output_block_open ();
output_line ("/* Table end */");
if (stmt) {
output_stmt (stmt);
output_block_open ();
if (at_end) {
output_source_reference (CB_PAIR_X (at_end), "AT END");
output_stmt (CB_PAIR_Y (at_end));
} else {
/* position is best guess here */
table->source_line++;
output_source_reference (table, "AT END");
table->source_line--;
output_line ("break;");
}
output_block_close ();

/* WHEN test */
output_stmt (whens);
output_newline ();

/* Iteration */
output_newline ();
{
/* Output source location as code,
especially for tracking adjustment of the index */
if (var) {
output_source_reference (var, "SEARCH VARYING");
} else {
/* output_source_reference is correct here but as we don't want
to trace internal code temporary disable source_location
because 3.x includes trace code */
int sav_fsl = cb_flag_source_location;
cb_flag_source_location = 0;
output_source_reference (table, "SEARCH VARYING internal");
cb_flag_source_location = sav_fsl;
}
}
output_prefix ();
output_integer (idx);
output ("++;");
output_newline ();
if (var && var != idx) {
output_move (idx, var);
}
output_line ("/* Iterate */");
/* End loop */
output_indent_level -= 2;
output_line ("}");
}

static void
output_search_all (cb_tree table, struct cb_field *p, cb_tree stmt,
cb_tree cond, cb_tree when)
output_search_all (cb_tree table, struct cb_field *p, cb_tree at_end,
cb_tree when_cond, cb_tree when_stmts)
{
cb_tree idx;

COB_UNUSED(table); /* to be handled later */

idx = CB_VALUE (p->index_list);
/* Header */
output_block_open ();
Expand All @@ -5515,40 +5532,76 @@ output_search_all (cb_tree table, struct cb_field *p, cb_tree stmt,
output_occurs (p);
output (" == 0) head = tail;");
output_newline ();
output_newline ();

/* Start loop */
last_line = -1; /* force statement reference output at begin of loop */
output_line ("for (;;)");
output_block_open ();

/* End test */
output_line ("if (head >= tail - 1)");
output_block_open ();
output_line ("/* Table end */");
if (stmt) {
output_stmt (stmt);
output_block_open ();
if (at_end) {
output_source_reference (CB_PAIR_X (at_end), "AT END");
output_stmt (CB_PAIR_Y (at_end));
} else {
/* position is best guess here */
table->source_line++;
output_source_reference (table, "AT END");
table->source_line--;
output_line ("break;");
}
output_block_close ();
output_newline ();

/* Next index */
{
/* Output source location as code,
especially for tracking adjustment of the index */
/* output_source_reference is correct here but as we don't want
to trace internal code temporary disable source_location
because 3.x includes trace code */
int sav_fsl = cb_flag_source_location;
cb_flag_source_location = 0;
output_source_reference (table, "SEARCH VARYING internal");
cb_flag_source_location = sav_fsl;
}
output_prefix ();
output_integer (idx);
output (" = (head + tail) / 2;");
output_newline ();
output_newline ();

/* WHEN test */
output_line ("/* WHEN */");
{
/* output_source_reference is correct here but due to 3.x
having source_location including trace code it would
heavily reduce SEARCH ALL performance
--> so temporary disable that here */
int sav_fsl = cb_flag_source_location;
cb_flag_source_location = 0;
output_source_reference (when_cond, "WHEN");
cb_flag_source_location = sav_fsl;
}
output_prefix ();
output ("if (");
output_cond (cond, 1);
output_cond (when_cond, 1);
output (")");
output_newline ();
output_block_open ();
output_stmt (when);
if (cb_flag_traceall || cb_old_trace) {
/* Output trace info */
/* note: this actually belongs only before the condition, but
for the trace code we add it here again */
output_trace_info (when_cond, "WHEN");
}
output_stmt (when_stmts);
output_block_close ();
output_newline ();

output_line ("/* setup for next binary search position */");
output_line ("if (ret < 0)");
output_prefix ();
output (" head = ");
Expand All @@ -5573,10 +5626,10 @@ output_search (struct cb_search *p)
/* TODO: Add run-time checks for the table, including ODO */

if (p->flag_all) {
output_search_all (p->table, fp, p->end_stmt,
output_search_all (p->table, fp, p->at_end,
CB_IF (p->whens)->test, CB_IF (p->whens)->stmt1);
} else {
output_search_whens (p->table, fp, p->end_stmt, p->var, p->whens);
output_search_whens (p->table, fp, p->at_end, p->var, p->whens);
}
}

Expand Down Expand Up @@ -7091,6 +7144,7 @@ output_perform (struct cb_perform *p)
output_newline ();
loop_counter++;
output_block_open ();
last_line = -1; /* force statement reference output at begin of loop */
output_perform_once (p);
output_block_close ();
break;
Expand All @@ -7117,6 +7171,7 @@ output_perform (struct cb_perform *p)
case CB_PERFORM_FOREVER:
output_line ("for (;;)");
output_block_open ();
last_line = -1; /* force statement reference output at begin of loop */
output_perform_once (p);
output_block_close ();
break;
Expand Down Expand Up @@ -7494,7 +7549,7 @@ output_cobol_info (cb_tree x)
const char *p = x->source_file;
output ("#line %d \"", x->source_line);
while(*p){
if( *p == '\\' ){
if (*p == '\\') {
output("%c",'\\');
}
output("%c",*p++);
Expand Down Expand Up @@ -7575,7 +7630,6 @@ output_section_info (struct cb_label *lp)
}
}


static void
output_trace_info (cb_tree x, const char *name)
{
Expand Down Expand Up @@ -7648,10 +7702,10 @@ output_source_reference (cb_tree tree, const char *stmt_name)
COB_SET_LINE_FILE(tree->source_line, lookup_source(tree->source_file)));
}
}
if (last_line != tree->source_line) {
/* Output source location as code */
output_line_and_trace_info (tree, stmt_name);
}
/* Output source location as code */
output_line_and_trace_info (tree, stmt_name);

last_line = tree->source_line;
}

static void
Expand Down Expand Up @@ -7922,8 +7976,8 @@ output_stmt (cb_tree x)
FIXME: postpone to actual DEBUGGING procedure,
using module->module_stmt there
*/
if (current_prog->flag_gen_debug &&
!p->flag_in_debug) {
if (current_prog->flag_gen_debug
&& !p->flag_in_debug) {
output_prefix ();
output ("memcpy (");
output_data (cb_debug_line);
Expand Down Expand Up @@ -8247,10 +8301,8 @@ output_stmt (cb_tree x)
}
} else if (ip->test->source_line) {
output_line ("/* Line: %-10d: WHEN */", ip->test->source_line);
if (last_line != ip->test->source_line) {
/* Output source location as code */
output_line_and_trace_info (ip->test, "WHEN");
}
/* Output source location as code */
output_line_and_trace_info (ip->test, "WHEN");
} else {
output_line ("/* WHEN */");
}
Expand Down
Loading

0 comments on commit 3d5aa86

Please sign in to comment.