#!/bin/sh
#
# replace symbolic debugging "stab" directives in cc generated assembly
# with the C source line they reference.  Works as-is on linux and NeXT
# may need adjustment for other versions of cc.
#
# Corey Satten, corey @ cac.washington.edu, 2/13/98
# http://staff.washington.edu/corey
#

trap : 13

AFILE=$1

CFILE=`sed -n '/\.stabs *"\([^"]*\.c\)".*/ {; s//\1/p; q; }' $AFILE`
case "$CFILE" in '')
    echo "$0: $1 must be compiled with -g" 0>&1; exit 1;;
esac

(   trap : 13
    cat $CFILE
    echo =-=-=
    cat $AFILE
) |

mawk '
    /^=-=-=$/		{++mode; next}
    mode==0		{line[NR] = $0; next}
    /^\.stabs +".+\.h"/	{ok=0; next}		# line nums suspect in includes
    /^\.stabs/		{ok=1; next}
    $1~/\.stab[nd]/ 	{
			if ( !ok ) next		# not a trustworthy line number
			n = split($2,a,",")
			n = a[3]
			if (n >= ptr) for (; ptr <= n; ++ptr)
					printf("\t\t%6d:%s\n", ptr, line[ptr])
			else if (n > 0)	printf("\t\t%6d:%s\n", -n,  line[n])
			next
			}
			{print}
    '
