xref: /freebsd/sys/kern/genassym.sh (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1#!/bin/sh
2
3usage()
4{
5	echo "usage: genassym [-o outfile] objfile"
6	exit 1
7}
8
9
10work()
11{
12	${NM:='nm'} ${NMFLAGS} "$1" | ${AWK:='awk'} '
13	/ C .*sign$/ {
14		sign = substr($1, length($1) - 3, 4)
15		sub("^0*", "", sign)
16		if (sign != "")
17			sign = "-"
18	}
19	/ C .*w0$/ {
20		w0 = substr($1, length($1) - 3, 4)
21	}
22	/ C .*w1$/ {
23		w1 = substr($1, length($1) - 3, 4)
24	}
25	/ C .*w2$/ {
26		w2 = substr($1, length($1) - 3, 4)
27	}
28	/ C .*w3$/ {
29		w3 = substr($1, length($1) - 3, 4)
30		w = w3 w2 w1 w0
31		sub("^0*", "", w)
32		if (w == "")
33			w = "0"
34		hex = ""
35		if (w != "0")
36			hex = "0x"
37		sub("w3$", "", $3)
38		# This still has minor problems representing INT_MIN, etc.
39		# E.g.,
40		# with 32-bit 2''s complement ints, this prints -0x80000000,
41		# which has the wrong type (unsigned int).
42		printf("#define\t%s\t%s%s%s\n", $3, sign, hex, w)
43	} '
44}
45
46
47#
48#MAIN PROGGRAM
49#
50use_outfile="no"
51while getopts "o:" option
52do
53	case "$option" in
54	o)	outfile="$OPTARG"
55		use_outfile="yes";;
56	*)	usage;;
57	esac
58done
59shift $((OPTIND - 1))
60case $# in
611)	;;
62*)	usage;;
63esac
64
65if [ "$use_outfile" = "yes" ]
66then
67	work "$1"  3>"$outfile" >&3 3>&-
68else
69	work "$1"
70fi
71
72