#! /bin/bash -f
#-----------------------------------------------------------------------
# $Id: blxchecker,v 1.4 2004/04/23 12:47:11 yasu Exp $
#   blx?? Commend check that follows command
#
#	blxeq   r0
#	beq	xx   Check this format
#
#-----------------------------------------------------------------------

if [ -z "$CWFOLDER_NITRO" ]; then
	export CWFOLDER_NITRO=C:/Program\ Files/Metrowerks/CodeWarrior\ for\ NITRO\ V0.4.1
fi

export PATH=$PATH:`cygpath "$CWFOLDER_NITRO/ARM_Tools/Command_Line_Tools"`

#
# Analyze *.o, *.a files that are disassembled by gawk.
#
/bin/find . -name '*.[ao]' -print -exec mwldarm.exe -w off -dis -nostdlib -show only,code,hex -proc arm9tdmi {} \; |
gawk '\

/^[.]/{				# What starts with . is a filename
	file  = $1;
	lines = 0;
	next;
}

$2 ~ /^[A-Za-z_][A-Za-z0-9_]*:/{# Acquire function name. (alphanumeric and colon
	fun   = $2;
	gsub( ":", "()", fun );
	lines = 0;
	next;
}

/^;/{				# Discard comment (which starts with ;)
	next;
}

{				# Analyzing
	if ( lines > 0 ){
		# cond field check
		if ( $2 ~ /^0x[0-9A-D].......$/ ){
			if ( !findln ){
				print "Warning: " file;
				print "> " fun;
				print codes;
			}
			print $0;
			findln ++;
		}
		lines --;
	}
}

# Find command bl[xx] or blx[xx]
$2 ~ /^0x[0-9A-D]12FFF3.$/ || 
( $2 ~ /^0x[0-9A-D]B......$/ && $3 !~ "dcd" ) {
	lines  = 30;		# Limit count just in case
	codes  = $0;
	findln = 0;
}

# Terminate search if a command to update flag, and a forced branch
$2 ~ /^0xE[0123][13579BDF].....$/ ||
$2 ~ /^0xE[89][4567CDEF].....$/   ||
$2 ~ /^0xE12FFF..$/ {
	lines = 0;
}
'

#-----------------------------------------------------------------------
# $Log: blxchecker,v $
# Revision 1.4  2004/04/23 12:47:11  yasu
# check by opcode bitfield
#
# Revision 1.3  2004/04/22 07:03:14  yasu
# Support *.a files
#
# Revision 1.2  2004/04/21 11:22:06  yasu
# Support codecheck as workaround for blxeq/ne problem
#
# Revision 1.1  2004/04/21 10:59:16  yasu
# Checker for illegal combination around blxeq/blxne
#
#-----------------------------------------------------------------------

