#! /bin/sh
#
# This script generates a disassembler output of the MGL source
#
# $Id: makedisass,v 1.1 2004/11/29 11:21:50 gkminix Exp $
#


# Check for mknbi program
if [ -z "$MKNBI" ]; then
	MKNBI="../mknbi"
fi
if [ ! -x "$MKNBI" ]; then
	echo "$0: unable to find mknbi program" >&2
	exit 1
fi


# Determine name of input file
if [ $# -ne 1 ]; then
	echo "usage: $0 <mgl file>" >&2
	exit 1
fi
INFILE="$1"


# Determine name of output file
FILEBASE=`echo "$INFILE" | sed 's/[.][^.]*$//'`
OUTFILE="${FILEBASE}.disass"


# Determine name of temporary file
TEMPFILE="${FILEBASE}.$$"
trap "rm -f $TEMPFILE" 0


# Compile the source file and record the code sizes
SIZES="`$MKNBI --print-sizes -t plain $INFILE $TEMPFILE`"
EXITCODE="$?"
if [ $EXITCODE -ne 0 ]; then
	echo "$0: mknbi returned with $EXITCODE" >&2
	exit 1
fi
STARTADDR=`echo "$SIZES" | sed 's/[ 	].*$//'`
ENDADDR=`echo "$SIZES" | sed 's/^.*[ 	]\+//'`


# Now produce the disassembly using objdump
# It has not been checked by configure if objdump exists and is able to
# disassemble x86 code! Do not use this script if a suitable objdump is
# not available.
objdump -D -m i8086 -b binary \
	--start-address=$STARTADDR --stop-address=$ENDADDR \
	$TEMPFILE > $OUTFILE

exit $?

