t40c term[] count fix
[minix.git] / usr.bin / genassym / genassym.sh
blob3f8f2beac76df7bb345fc4daf0977ecb71ef228a
1 #!/bin/sh -
2 # $NetBSD: genassym.sh,v 1.7 2011/07/05 05:19:02 matt Exp $
4 # Copyright (c) 1997 Matthias Pfaller.
5 # All rights reserved.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
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.
28 progname=${0}
29 : ${AWK:=awk}
31 ccode=0 # generate temporary C file, compile it, execute result
32 fcode=0 # generate Forth code
34 usage()
37 echo "usage: ${progname} [-c | -f] -- compiler command" >&2
40 while getopts cf i
42 case "$i" in
44 ccode=1
47 fcode=1
49 esac
50 done
51 shift $(($OPTIND - 1))
52 if [ $# -eq 0 ]; then
53 usage
54 exit 1
57 # Deal with any leading environment settings..
59 while [ "$1" ]
61 case "$1" in
62 *=*)
63 eval export "$1"
64 shift
67 break
69 esac
70 done
72 genassym_temp=/tmp/genassym.$$
74 if ! mkdir $genassym_temp; then
75 echo "${progname}: unable to create temporary directory" >&2
76 exit 1
78 trap "rm -rf $genassym_temp" 0 1 2 3 15
80 $AWK '
81 BEGIN {
82 printf("#if __GNUC__ >= 4\n");
83 printf("#define offsetof(type, member) __builtin_offsetof(type, member)\n");
84 printf("#else\n");
85 printf("#define offsetof(type, member) ((size_t)(&((type *)0)->member))\n");
86 printf("#endif\n");
87 defining = 0;
88 type = "long";
89 asmtype = "n";
90 asmprint = "";
94 doing_member = 0;
97 $0 ~ /^[ \t]*#.*/ || $0 ~ /^[ \t]*$/ {
98 # Just ignore comments and empty lines
99 next;
102 $0 ~ /^config[ \t]/ {
103 type = $2;
104 asmtype = $3;
105 asmprint = $4;
106 next;
109 /^include[ \t]/ {
110 if (defining != 0) {
111 defining = 0;
112 printf("}\n");
114 printf("#%s\n", $0);
115 next;
118 $0 ~ /^if[ \t]/ ||
119 $0 ~ /^ifdef[ \t]/ ||
120 $0 ~ /^ifndef[ \t]/ ||
121 $0 ~ /^else/ ||
122 $0 ~ /^elif[ \t]/ ||
123 $0 ~ /^endif/ {
124 printf("#%s\n", $0);
125 next;
128 /^struct[ \t]/ {
129 structname = $2;
130 $0 = "define " structname "_SIZEOF sizeof(struct " structname ")";
131 # fall through
134 /^member[ \t]/ {
135 if (NF > 2)
136 $0 = "define " $2 " offsetof(struct " structname ", " $3 ")";
137 else
138 $0 = "define " $2 " offsetof(struct " structname ", " $2 ")";
139 doing_member = 1;
140 # fall through
143 /^export[ \t]/ {
144 $0 = "define " $2 " " $2;
145 # fall through
148 /^define[ \t]/ {
149 if (defining == 0) {
150 defining = 1;
151 printf("void f" FNR "(void);\n");
152 printf("void f" FNR "(void) {\n");
153 if (ccode)
154 call[FNR] = "f" FNR;
155 defining = 1;
157 value = $0
158 gsub("^define[ \t]+[A-Za-z_][A-Za-z_0-9]*[ \t]+", "", value)
159 if (ccode)
160 printf("printf(\"#define " $2 " %%ld\\n\", (%s)" value ");\n", type);
161 else if (fcode) {
162 if (doing_member)
163 printf("__asm(\"XYZZY : %s d# %%%s0 + ;\" : : \"%s\" (%s));\n", $2, asmprint, asmtype, value);
164 else
165 printf("__asm(\"XYZZY d# %%%s0 constant %s\" : : \"%s\" (%s));\n", asmprint, $2, asmtype, value);
166 } else
167 printf("__asm(\"XYZZY %s %%%s0\" : : \"%s\" (%s));\n", $2, asmprint, asmtype, value);
168 next;
171 /^quote[ \t]/ {
172 gsub("^quote[ \t]+", "");
173 print;
174 next;
178 printf("syntax error in line %d\n", FNR) >"/dev/stderr";
179 exit(1);
182 END {
183 if (defining != 0) {
184 defining = 0;
185 printf("}\n");
187 if (ccode) {
188 printf("int main(int argc, char **argv) {");
189 for (i in call)
190 printf(call[i] "();");
191 printf("return(0); }\n");
194 ' ccode=$ccode fcode=$fcode > ${genassym_temp}/assym.c || exit 1
196 if [ $ccode = 1 ] ; then
197 "$@" ${genassym_temp}/assym.c -o ${genassym_temp}/genassym && \
198 ${genassym_temp}/genassym
199 elif [ $fcode = 1 ]; then
200 # Kill all of the "#" and "$" modifiers; locore.s already
201 # prepends the correct "constant" modifier.
202 "$@" -S ${genassym_temp}/assym.c -o - | sed -e 's/\$//g' | \
203 sed -n 's/.*XYZZY//gp'
204 else
205 # Kill all of the "#" and "$" modifiers; locore.s already
206 # prepends the correct "constant" modifier.
207 "$@" -S ${genassym_temp}/assym.c -o - > \
208 ${genassym_temp}/genassym.out && \
209 sed -e 's/#//g' -e 's/\$//g' < ${genassym_temp}/genassym.out | \
210 sed -n 's/.*XYZZY/#define/gp'