2 # $NetBSD: genassym.sh,v 1.5 2009/10/18 18:14:00 snj Exp $
4 # Copyright (c) 1997 Matthias Pfaller.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 ccode
=0 # generate temporary C file, compile it, execute result
32 fcode
=0 # generate Forth code
37 echo "usage: ${progname} [-c | -f] -- compiler command" >&2
51 shift $
(($OPTIND - 1))
57 # Deal with any leading environment settings..
72 genassym_temp
=/tmp
/genassym.$$
74 if ! mkdir
$genassym_temp; then
75 echo "${progname}: unable to create temporary directory" >&2
78 trap "rm -rf $genassym_temp" 0 1 2 3 15
82 printf("#define offsetof(type, member) ((size_t)(&((type *)0)->member))\n");
93 $0 ~ /^[ \t]*#.*/ || $0 ~ /^[ \t]*$/ {
94 # Just ignore comments and empty lines
115 $0 ~ /^ifdef[ \t]/ ||
116 $0 ~ /^ifndef[ \t]/ ||
126 $0 = "define " structname "_SIZEOF sizeof(struct " structname ")";
132 $0 = "define " $2 " offsetof(struct " structname ", " $3 ")";
134 $0 = "define " $2 " offsetof(struct " structname ", " $2 ")";
140 $0 = "define " $2 " " $2;
147 printf("void f" FNR "(void);\n");
148 printf("void f" FNR "(void) {\n");
154 gsub("^define[ \t]+[A-Za-z_][A-Za-z_0-9]*[ \t]+", "", value)
156 printf("printf(\"#define " $2 " %%ld\\n\", (%s)" value ");\n", type);
159 printf("__asm(\"XYZZY : %s d# %%%s0 + ;\" : : \"%s\" (%s));\n", $2, asmprint, asmtype, value);
161 printf("__asm(\"XYZZY d# %%%s0 constant %s\" : : \"%s\" (%s));\n", asmprint, $2, asmtype, value);
163 printf("__asm(\"XYZZY %s %%%s0\" : : \"%s\" (%s));\n", $2, asmprint, asmtype, value);
168 gsub("^quote[ \t]+", "");
174 printf("syntax error in line %d\n", FNR) >"/dev/stderr";
184 printf("int main(int argc, char **argv) {");
186 printf(call[i] "();");
187 printf("return(0); }\n");
190 ' ccode
=$ccode fcode
=$fcode > ${genassym_temp}/assym.c ||
exit 1
192 if [ $ccode = 1 ] ; then
193 "$@" ${genassym_temp}/assym.c
-o ${genassym_temp}/genassym
&& \
194 ${genassym_temp}/genassym
195 elif [ $fcode = 1 ]; then
196 # Kill all of the "#" and "$" modifiers; locore.s already
197 # prepends the correct "constant" modifier.
198 "$@" -S ${genassym_temp}/assym.c
-o - |
sed -e 's/\$//g' | \
199 sed -n 's/.*XYZZY//gp'
201 # Kill all of the "#" and "$" modifiers; locore.s already
202 # prepends the correct "constant" modifier.
203 "$@" -S ${genassym_temp}/assym.c
-o - > \
204 ${genassym_temp}/genassym.out
&& \
205 sed -e 's/#//g' -e 's/\$//g' < ${genassym_temp}/genassym.out | \
206 sed -n 's/.*XYZZY/#define/gp'