1997-08-20 10:58:10 +04:00
|
|
|
# $NetBSD: genassym.sh,v 1.8 1997/08/20 06:58:10 mikel Exp $
|
1997-01-30 12:37:20 +03:00
|
|
|
|
|
|
|
#
|
|
|
|
# Copyright (c) 1997 Matthias Pfaller.
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions
|
|
|
|
# are met:
|
|
|
|
# 1. Redistributions of source code must retain the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer.
|
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# 3. All advertising materials mentioning features or use of this software
|
|
|
|
# must display the following acknowledgement:
|
|
|
|
# This product includes software developed by Matthias Pfaller.
|
|
|
|
# 4. The name of the author may not be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written permission
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
#
|
|
|
|
|
|
|
|
# If first argument is -c, create a temporary C file,
|
|
|
|
# compile it and execute the result.
|
|
|
|
|
1997-03-14 01:47:20 +03:00
|
|
|
awk=${AWK:-awk}
|
1997-02-07 01:16:12 +03:00
|
|
|
|
1997-01-30 12:37:20 +03:00
|
|
|
if [ $1 = '-c' ] ; then
|
|
|
|
shift
|
|
|
|
ccode=1
|
|
|
|
else
|
|
|
|
ccode=0
|
|
|
|
fi
|
1997-02-01 00:38:16 +03:00
|
|
|
|
1997-01-30 12:37:20 +03:00
|
|
|
trap "rm -f /tmp/$$.c /tmp/genassym.$$" 0 1 2 3 15
|
|
|
|
|
1997-03-14 01:47:20 +03:00
|
|
|
$awk '
|
1997-01-30 12:37:20 +03:00
|
|
|
BEGIN {
|
|
|
|
printf("#ifndef _KERNEL\n#define _KERNEL\n#endif\n");
|
|
|
|
printf("#define offsetof(type, member) ((size_t)(&((type *)0)->member))\n");
|
|
|
|
defining = 0;
|
|
|
|
}
|
|
|
|
|
1997-02-07 01:16:12 +03:00
|
|
|
$0 ~ /^[ \t]*#.*/ || $0 ~ /^[ \t]*$/ {
|
1997-02-01 00:56:28 +03:00
|
|
|
# Just ignore comments and empty lines
|
|
|
|
next;
|
1997-01-30 12:37:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/^include[ \t]/ {
|
1997-02-07 01:16:12 +03:00
|
|
|
if (defining != 0) {
|
|
|
|
defining = 0;
|
|
|
|
printf("}\n");
|
|
|
|
}
|
1997-01-30 12:37:20 +03:00
|
|
|
printf("#%s\n", $0);
|
1997-02-01 00:56:28 +03:00
|
|
|
next;
|
1997-01-30 12:37:20 +03:00
|
|
|
}
|
|
|
|
|
1997-02-07 01:16:12 +03:00
|
|
|
$0 ~ /^if[ \t]/ ||
|
|
|
|
$0 ~ /^ifdef[ \t]/ ||
|
|
|
|
$0 ~ /^ifndef[ \t]/ ||
|
|
|
|
$0 ~ /^else/ ||
|
|
|
|
$0 ~ /^elif[ \t]/ ||
|
|
|
|
$0 ~ /^endif/ {
|
1997-01-30 12:37:20 +03:00
|
|
|
printf("#%s\n", $0);
|
1997-02-01 00:56:28 +03:00
|
|
|
next;
|
1997-01-30 12:37:20 +03:00
|
|
|
}
|
|
|
|
|
1997-06-25 07:09:06 +04:00
|
|
|
/^struct[ \t]/ {
|
|
|
|
structname = $2;
|
|
|
|
$0 = "define " structname "_SIZEOF sizeof(struct " structname ")";
|
1997-08-20 10:58:10 +04:00
|
|
|
# fall through
|
1997-06-25 07:09:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/^member[ \t]/ {
|
|
|
|
$0 = "define " $2 " offsetof(struct " structname ", " $2 ")";
|
1997-08-20 10:58:10 +04:00
|
|
|
# fall through
|
1997-06-25 07:09:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/^export[ \t]/ {
|
|
|
|
$0 = "define " $2 " " $2;
|
1997-08-20 10:58:10 +04:00
|
|
|
# fall through
|
1997-06-25 07:09:06 +04:00
|
|
|
}
|
|
|
|
|
1997-01-30 12:37:20 +03:00
|
|
|
/^define[ \t]/ {
|
1997-02-07 01:16:12 +03:00
|
|
|
if (defining == 0) {
|
|
|
|
defining = 1;
|
|
|
|
printf("void f" FNR "(void);\n");
|
|
|
|
printf("void f" FNR "() {\n");
|
|
|
|
if (ccode)
|
|
|
|
call[FNR] = "f" FNR;
|
|
|
|
defining = 1;
|
|
|
|
}
|
1997-01-30 12:37:20 +03:00
|
|
|
value = $0
|
|
|
|
gsub("^define[ \t]+[A-Za-z_][A-Za-z_0-9]*[ \t]+", "", value)
|
|
|
|
if (ccode)
|
|
|
|
printf("printf(\"#define " $2 " %%ld\\n\", (long)" value ");\n");
|
|
|
|
else
|
|
|
|
printf("__asm(\"XYZZY %s %%0\" : : \"n\" (%s));\n", $2, value);
|
1997-02-01 00:56:28 +03:00
|
|
|
next;
|
1997-01-30 12:37:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/^quote[ \t]/ {
|
1997-02-01 00:38:16 +03:00
|
|
|
gsub("^quote[ \t]+", "");
|
|
|
|
print;
|
1997-02-01 00:56:28 +03:00
|
|
|
next;
|
1997-01-30 12:37:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
1997-02-01 00:56:28 +03:00
|
|
|
printf("syntax error in line %d\n", FNR) >"/dev/stderr";
|
|
|
|
exit(1);
|
1997-01-30 12:37:20 +03:00
|
|
|
}
|
1997-02-07 01:16:12 +03:00
|
|
|
|
1997-01-30 12:37:20 +03:00
|
|
|
END {
|
1997-02-07 01:16:12 +03:00
|
|
|
if (defining != 0) {
|
|
|
|
defining = 0;
|
|
|
|
printf("}\n");
|
|
|
|
}
|
1997-01-30 12:37:20 +03:00
|
|
|
if (ccode) {
|
|
|
|
printf("int main(int argc, char **argv) {");
|
|
|
|
for (i in call)
|
|
|
|
printf(call[i] "();");
|
1997-02-01 00:38:16 +03:00
|
|
|
printf("return(0); }\n");
|
1997-01-30 12:37:20 +03:00
|
|
|
}
|
|
|
|
}
|
1997-02-07 01:16:12 +03:00
|
|
|
' ccode=$ccode > /tmp/$$.c || exit 1
|
|
|
|
|
1997-01-30 12:37:20 +03:00
|
|
|
if [ $ccode = 1 ] ; then
|
|
|
|
"$@" /tmp/$$.c -o /tmp/genassym.$$ && /tmp/genassym.$$
|
|
|
|
else
|
1997-02-27 02:44:24 +03:00
|
|
|
# Kill all of the "#" and "$" modifiers; locore.s already
|
|
|
|
# prepends the correct "constant" modifier.
|
|
|
|
"$@" -S /tmp/$$.c -o -| sed -e 's/#//g' -e 's/\$//g' | \
|
|
|
|
sed -n 's/.*XYZZY/#define/gp'
|
1997-01-30 12:37:20 +03:00
|
|
|
fi
|