#!/usr/bin/awk -f
#
# This script extracts information about stack frame sizes from its input,
# which should be a C source file generated by the Mercury compiler in an
# LLDS grade, preceded by a line of the form "VERSION_NUMBER n".
#
# Its output will contain one line for each procedure that has a stack frame.
# That line will contain five fields:
#
# - the version number
# - the size of the procedure's stack frame in words
# - the name of the stack the stack frame is on, either "det" or "non"
# - the nature of the procedure, either "pred" or "func"
# - the name of the procedure, including module name, arity and mode number

$1 == "VERSION_NUMBER"		{
					version = $2;
					next;
				}
$1 == "MR_BEGIN_CODE"		{
					in_valid_range = 1;
					next;
				}
$1 == "MR_END_CODE"		{
					in_valid_range = 0;
					next;
				}
$1 ~ "MR_incr_sp_push_msg\(.*"	{
					gsub(" = ", "=", $0);
					if (in_valid_range) {
						size = $1;
						pf = $2;
						name = $3;
						gsub("MR_incr_sp_push_msg\(",
							"", size);
						gsub("[\"\(\),; ]", "", size);
						gsub("[\"\(\),; ]", "", pf);
						gsub("[\"\(\),; ]", "", name);

						printf "%s %2d det %s %s\n",
							version,
							size, pf, name;

						# frameopt can duplicate
						# the procedure prologue
						in_valid_range = 0;
					}
				}
$1 ~ "MR_mkframe\(.*"		{
					gsub(" = ", "=", $0);
					if (in_valid_range) {
						pf = $1;
						name = $2;
						size = $3;
						gsub("MR_mkframe\(", "", pf);
						gsub("[\"\(\),; ]", "", pf);
						gsub("[\"\(\),; ]", "", name);
						gsub("[\"\(\),; ]", "", size);

						printf "%s %2d non %s %s\n",
							version,
							size + 5, pf, name;

						# frameopt can duplicate
						# the procedure prologue
						in_valid_range = 0;
					}
				}
