FreeBSD/Linux Kernel Cross Reference
sys/kern/genassym.sh
1 # $NetBSD: genassym.sh,v 1.13 2005/02/26 21:34:55 perry Exp $
2
3 #
4 # Copyright (c) 1997 Matthias Pfaller.
5 # All rights reserved.
6 #
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.
15 # 3. All advertising materials mentioning features or use of this software
16 # must display the following acknowledgement:
17 # This product includes software developed by Matthias Pfaller.
18 # 4. The name of the author may not be used to endorse or promote products
19 # derived from this software without specific prior written permission
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #
32
33 # If first argument is -c, create a temporary C file,
34 # compile it and execute the result.
35
36 awk=${AWK:-awk}
37
38 if [ "$1" = '-c' ] ; then
39 shift
40 ccode=1
41 else
42 ccode=0
43 fi
44
45 # Deal with any leading environment settings..
46
47 while [ "$1" ]
48 do
49 case "$1" in
50 *=*)
51 eval export "$1"
52 shift
53 ;;
54 *)
55 break
56 ;;
57 esac
58 done
59
60 trap "rm -f /tmp/$$.c /tmp/genassym.$$" 0 1 2 3 15
61
62 $awk '
63 BEGIN {
64 printf("#ifndef _KERNEL\n#define _KERNEL\n#endif\n");
65 printf("#define offsetof(type, member) ((size_t)(&((type *)0)->member))\n");
66 defining = 0;
67 type = "long";
68 asmtype = "n";
69 asmprint = "";
70 }
71
72 $0 ~ /^[ \t]*#.*/ || $0 ~ /^[ \t]*$/ {
73 # Just ignore comments and empty lines
74 next;
75 }
76
77 $0 ~ /^config[ \t]/ {
78 type = $2;
79 asmtype = $3;
80 asmprint = $4;
81 next;
82 }
83
84 /^include[ \t]/ {
85 if (defining != 0) {
86 defining = 0;
87 printf("}\n");
88 }
89 printf("#%s\n", $0);
90 next;
91 }
92
93 $0 ~ /^if[ \t]/ ||
94 $0 ~ /^ifdef[ \t]/ ||
95 $0 ~ /^ifndef[ \t]/ ||
96 $0 ~ /^else/ ||
97 $0 ~ /^elif[ \t]/ ||
98 $0 ~ /^endif/ {
99 printf("#%s\n", $0);
100 next;
101 }
102
103 /^struct[ \t]/ {
104 structname = $2;
105 $0 = "define " structname "_SIZEOF sizeof(struct " structname ")";
106 # fall through
107 }
108
109 /^member[ \t]/ {
110 if (NF > 2)
111 $0 = "define " $2 " offsetof(struct " structname ", " $3 ")";
112 else
113 $0 = "define " $2 " offsetof(struct " structname ", " $2 ")";
114 # fall through
115 }
116
117 /^export[ \t]/ {
118 $0 = "define " $2 " " $2;
119 # fall through
120 }
121
122 /^define[ \t]/ {
123 if (defining == 0) {
124 defining = 1;
125 printf("void f" FNR "(void);\n");
126 printf("void f" FNR "() {\n");
127 if (ccode)
128 call[FNR] = "f" FNR;
129 defining = 1;
130 }
131 value = $0
132 gsub("^define[ \t]+[A-Za-z_][A-Za-z_0-9]*[ \t]+", "", value)
133 if (ccode)
134 printf("printf(\"#define " $2 " %%ld\\n\", (%s)" value ");\n", type);
135 else
136 printf("__asm(\"XYZZY %s %%%s0\" : : \"%s\" (%s));\n", $2, asmprint, asmtype, value);
137 next;
138 }
139
140 /^quote[ \t]/ {
141 gsub("^quote[ \t]+", "");
142 print;
143 next;
144 }
145
146 {
147 printf("syntax error in line %d\n", FNR) >"/dev/stderr";
148 exit(1);
149 }
150
151 END {
152 if (defining != 0) {
153 defining = 0;
154 printf("}\n");
155 }
156 if (ccode) {
157 printf("int main(int argc, char **argv) {");
158 for (i in call)
159 printf(call[i] "();");
160 printf("return(0); }\n");
161 }
162 }
163 ' ccode=$ccode > /tmp/$$.c || exit 1
164
165 if [ $ccode = 1 ] ; then
166 "$@" /tmp/$$.c -o /tmp/genassym.$$ && /tmp/genassym.$$
167 else
168 # Kill all of the "#" and "$" modifiers; locore.s already
169 # prepends the correct "constant" modifier.
170 "$@" -S /tmp/$$.c -o - > /tmp/genassym.$$ && \
171 sed -e 's/#//g' -e 's/\$//g' < /tmp/genassym.$$ | \
172 sed -n 's/.*XYZZY/#define/gp'
173 fi
Cache object: 1b592d97374ddd9277a4161d552dd750
|