#!/bin/sh
###############################################################################
#
# $Id: build-macros,v 1.1 1998/04/02 20:52:18 bje Exp $
#
# Copyight (C) 1997 Cygnus Solutions, Inc.
#
# Description:
# This script takes a couple of pre-compiled C programs and inspects the 
# assembler output to try and guess what the mneumonics and pseudo-op names
# are for a particular assembly syntax.
#
# Usage: build-defns <macro filename> 
#
# If the script gets it wrong, you can always edit the .m4 file by hand!
#
###############################################################################

outfile=$1.m4
asmsrc=a.s
functionname=funcb
globalvar=ina
staticvar=hiddena

##############################################################################
# Test $* for non-emptiness.  If it is empty, then output an error message to
# indicate that something went wrong when trying to locate some piece of
# information in the assembly source.
##############################################################################

check()
{
  error=$1
  shift
  if [ "$*" = "" ] ; then
    echo Unable to determine $error!
    rm $outfile
    exit
  fi
}

##############################################################################
# Try and locate the pseudo-op (out of a possible number of lines of matching
# text) that indicates that a data object is static.
##############################################################################

static_keyword()
{
  for token in $* ; do
    token=`echo $token | awk -F, '{ print $1 }'`
    if [ "$token" = "hiddena" -a "$last" != "$globalop" ] ; then 
      staticop=$last
    fi
    last=$token
  done
}

#######################################################################
# Emit some progress indicators to the user on stdout and build the M4
# macro file.
#######################################################################

emit()
{
  echo We use \`$3\' to $2.
  echo "define(\`$1', \`"$3"')" >> $outfile
} 

###########
# mainline
###########

if [ $# = 0 ] ; then
  echo "Usage: $0 <filename>.m4"
  exit 1
else
  rm $outfile
fi

if [ ! -f $asmsrc ] ; then
  echo File not found: $asmsrc
  exit 1
fi

result="#"
check $result "comment character"
commchar=$result
emit "_COMMCHAR" "start end-of-line comments" $commchar

result=`grep $functionname $asmsrc`
check $result "jump opcode mneumonic"
callop=`echo $result | awk '{ print $1 }'`
emit "_CALLOP" "make subroutine calls" $callop

result=`grep $globalvar $asmsrc | grep -v 12`
check $result "pseudo-op for declaring global data"
globalop=`echo $result | awk '{ print $1 }'`
emit "_GLOBALOP" "declare global data" $globalop

result=`grep $staticvar $asmsrc | grep -v 36`
check $result "pseudo-op for declaring static data"
static_keyword $result
emit "_STATICOP" "declare static data" $staticop

result=".macro"
check $result "macro pseudo-op"
macrop=$result
emit "_MACROP" "define macros" $macrop

result=".L"
check $result "local label prefix"
macrop=$result
emit "_LOCLABEL" "prefix local labels" $macrop

result={symbol-name}":"
check $result "label pattern"
label=$result
emit "_LABEL" "specify labels" $label
echo "(except on HPPA, where a label may be ^{symbol-name}{ws})"
