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