Skip to content

Commit

Permalink
Add profiling of CALL statements
Browse files Browse the repository at this point in the history
  • Loading branch information
lefessan committed Jan 5, 2024
1 parent 1409c8d commit 1a9c9bb
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 110 deletions.
119 changes: 75 additions & 44 deletions cobc/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -4298,12 +4298,7 @@ output_funcall_item (cb_tree x, const int i, unsigned int func_nolitcast)

/* Use constant strings to replace string comparisons by more
* efficient pointer comparisons */
static const char *cob_prof_exit_paragraph_str = "cob_prof_exit_paragraph";
static const char *cob_prof_exit_section_str = "cob_prof_exit_section";
static const char *cob_prof_enter_paragraph_str = "cob_prof_enter_paragraph";
static const char *cob_prof_enter_section_str = "cob_prof_enter_section";
static const char *cob_prof_use_paragraph_entry_str = "cob_prof_use_paragraph_entry";
static const char *cob_prof_stayin_paragraph_str = "cob_prof_stayin_paragraph";
static const char *cob_prof_function_call_str = "cob_prof_function_call";

/* Allocate a procedure description record and add it at the end of
* the procedure_list of the current program. The index of the
Expand All @@ -4323,7 +4318,7 @@ procedure_list_add (
int ret = program->procedure_list_len ;

p = cobc_main_malloc (sizeof (struct cb_procedure_list));
p->proc.text = cobc_main_strdup (text);
if (text){ p->proc.text = cobc_main_strdup (text); }
p->proc.kind = kind;
p->proc.file = file;
p->proc.line = line;
Expand Down Expand Up @@ -4356,9 +4351,10 @@ cb_build_prof_call (enum cb_prof_call prof_call,
struct cb_program *program,
struct cb_label *section,
struct cb_label *paragraph,
const char *entry)
const char *entry,
cb_tree location)
{
const char *func_name;
const char *func_name = cob_prof_function_call_str;
int func_arg1;
int func_arg2 = -1;

Expand Down Expand Up @@ -4389,7 +4385,6 @@ cb_build_prof_call (enum cb_prof_call prof_call,
section->common.source_file,
section->common.source_line);
program->prof_current_paragraph = -1;
func_name = cob_prof_enter_section_str;
func_arg1 = program->prof_current_section;
break;

Expand All @@ -4404,7 +4399,6 @@ cb_build_prof_call (enum cb_prof_call prof_call,
program->prof_current_section,
paragraph->common.source_file,
paragraph->common.source_line);
func_name = cob_prof_enter_paragraph_str;
func_arg1 = program->prof_current_paragraph;
break;

Expand All @@ -4413,13 +4407,11 @@ cb_build_prof_call (enum cb_prof_call prof_call,
* re-registering the entry into the paragraph. */
case COB_PROF_STAYIN_PARAGRAPH:

func_name = cob_prof_stayin_paragraph_str;
func_arg1 = program->prof_current_paragraph;
break;

case COB_PROF_USE_PARAGRAPH_ENTRY:

func_name = cob_prof_use_paragraph_entry_str;
func_arg1 = program->prof_current_paragraph;
func_arg2 =
procedure_list_add (
Expand All @@ -4428,13 +4420,12 @@ cb_build_prof_call (enum cb_prof_call prof_call,
entry,
/* section field of entry is in fact its paragraph */
program->prof_current_paragraph,
section->common.source_file,
section->common.source_line);
location->source_file,
location->source_line);
break;

case COB_PROF_EXIT_PARAGRAPH:

func_name = cob_prof_exit_paragraph_str;
func_arg1 = program->prof_current_paragraph;
/* Do not reinitialize, because we may have several of these
EXIT_PARAGRAPH, for example at EXIT SECTION.
Expand All @@ -4443,17 +4434,42 @@ cb_build_prof_call (enum cb_prof_call prof_call,

case COB_PROF_EXIT_SECTION:

func_name = cob_prof_exit_section_str;
func_arg1 = program->prof_current_section;
/* reset current paragraph and section */
program->prof_current_section = -1;
program->prof_current_paragraph = -1;
break;

case COB_PROF_ENTER_CALL:

/* allocate call record and remember current call */
program->prof_current_call =
procedure_list_add (
program,
COB_PROF_PROCEDURE_CALL,
NULL,
program->prof_current_paragraph,
paragraph->common.source_file,
paragraph->common.source_line);
func_arg1 = program->prof_current_call;
break;

case COB_PROF_EXIT_CALL:

/* We need to patch the last procedure to add the callee name and loc */
program->procedure_list_last->proc.text = cobc_main_strdup (entry);
program->procedure_list_last->proc.file = location->source_file;
program->procedure_list_last->proc.line = location->source_line;

func_arg1 = program->prof_current_call;
program->prof_current_call = -1;
break;

}
if (func_arg2 < 0){
return CB_BUILD_FUNCALL_1 (func_name, cb_int (func_arg1));
return CB_BUILD_FUNCALL_2 (func_name, cb_int (prof_call), cb_int (func_arg1));
}
return CB_BUILD_FUNCALL_2 (func_name, cb_int (func_arg1), cb_int (func_arg2));
return CB_BUILD_FUNCALL_3 (func_name, cb_int (prof_call), cb_int (func_arg1), cb_int (func_arg2));
}

static void
Expand All @@ -4471,37 +4487,52 @@ output_funcall (cb_tree x)
return;
}

if ( cb_flag_prof ) {
if ( p->name == cob_prof_exit_paragraph_str
|| p->name == cob_prof_enter_section_str
|| p->name == cob_prof_exit_section_str ) {
int proc_idx = CB_INTEGER(p->argv[0])->val;
output ("%s (prof_info, %d)", p->name, proc_idx);
return;
}
if ( cb_flag_prof && p->name == cob_prof_function_call_str ) {

if ( p->name == cob_prof_enter_paragraph_str ) {
int proc_idx = CB_INTEGER(p->argv[0])->val;
output ("%s (prof_info, %d);\n", p->name, proc_idx);
output (" cob_prof_fallthrough_entry = 0");
return;
}
int proc_idx ;

switch ( CB_INTEGER (p->argv[0])->val ){

if (p->name == cob_prof_use_paragraph_entry_str){
int paragraph_idx = CB_INTEGER(p->argv[0])->val;
int entry_idx = CB_INTEGER(p->argv[1])->val;
case COB_PROF_EXIT_PARAGRAPH:
proc_idx = CB_INTEGER(p->argv[1])->val;
output ("cob_prof_exit_procedure (prof_info, %d)", proc_idx);
break;
case COB_PROF_ENTER_SECTION:
proc_idx = CB_INTEGER(p->argv[1])->val;
output ("cob_prof_enter_section (prof_info, %d)", proc_idx);
break;
case COB_PROF_EXIT_SECTION:
proc_idx = CB_INTEGER(p->argv[1])->val;
output ("cob_prof_exit_section (prof_info, %d)", proc_idx);
break;
case COB_PROF_ENTER_CALL:
proc_idx = CB_INTEGER(p->argv[1])->val;
output ("cob_prof_enter_procedure (prof_info, %d)", proc_idx);
break;
case COB_PROF_EXIT_CALL:
proc_idx = CB_INTEGER(p->argv[1])->val;
output ("cob_prof_exit_procedure (prof_info, %d)", proc_idx);
break;
case COB_PROF_ENTER_PARAGRAPH:
proc_idx = CB_INTEGER(p->argv[1])->val;
output ("cob_prof_enter_procedure (prof_info, %d);\n", proc_idx);
output (" cob_prof_fallthrough_entry = 0");
break;
case COB_PROF_USE_PARAGRAPH_ENTRY: {
int paragraph_idx = CB_INTEGER(p->argv[1])->val;
int entry_idx = CB_INTEGER(p->argv[2])->val;
output ("if (!cob_prof_fallthrough_entry){\n");
output ("\t%s (prof_info, %d, %d);\n",
cob_prof_use_paragraph_entry_str, paragraph_idx, entry_idx);
output ("\tcob_prof_use_paragraph_entry (prof_info, %d, %d);\n",
paragraph_idx, entry_idx);
output (" }\n");
output (" cob_prof_fallthrough_entry = 0");
return;
break;
}

if (p->name == cob_prof_stayin_paragraph_str){
case COB_PROF_STAYIN_PARAGRAPH:
output ("cob_prof_fallthrough_entry = 1");
return;
break;
}
return ;
}


Expand Down Expand Up @@ -12396,7 +12427,7 @@ output_internal_function (struct cb_program *prog, cb_tree parameter_list)
we keep pointers to static data there. */
output_line ("if (prof_info){ module->flag_no_phys_canc = 1; }");

output_line ("cob_prof_enter_program (prof_info);");
output_line ("cob_prof_enter_procedure (prof_info, 0);");
}
if (cb_flag_stack_extended) {
/* entry marker = first frameptr is the one with
Expand Down Expand Up @@ -12493,7 +12524,7 @@ output_internal_function (struct cb_program *prog, cb_tree parameter_list)
}
}
if (cb_flag_prof){
output_line ("cob_prof_exit_program (prof_info);");
output_line ("cob_prof_exit_procedure (prof_info, 0);");
}
if (!prog->flag_recursive) {
output_line ("/* Decrement module active count */");
Expand Down
Loading

0 comments on commit 1a9c9bb

Please sign in to comment.