binutils214 stage 2/4.
[dragonfly.git] / sys / tools / vnode_if.awk
1 #!/usr/bin/awk -f
2
3 #
4 # Copyright (c) 1992, 1993
5 #       The Regents of the University of California.  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 the University of
18 #       California, Berkeley and its contributors.
19 # 4. Neither the name of the University nor the names of its contributors
20 #    may be used to endorse or promote products derived from this software
21 #    without specific prior written permission.
22 #
23 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 # ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 # SUCH DAMAGE.
34 #
35 #       @(#)vnode_if.sh 8.1 (Berkeley) 6/10/93
36 # $FreeBSD: src/sys/tools/vnode_if.awk,v 1.39 2003/06/22 21:20:06 truckman Exp $
37 # $DragonFly: src/sys/tools/Attic/vnode_if.awk,v 1.4 2003/11/22 21:12:35 asmodai Exp $
38 #
39 # Script to produce VFS front-end sugar.
40 #
41 # usage: vnode_if.awk <srcfile> [-c | -h]
42 #       (where <srcfile> is currently /sys/kern/vnode_if.src)
43 #
44
45 function usage()
46 {
47         print "usage: vnode_if.awk <srcfile> [-c|-h]";
48         exit 1;
49 }
50
51 function die(msg, what)
52 {
53         printf msg "\n", what > "/dev/stderr";
54         exit 1;
55 }
56
57 function t_spc(type)
58 {
59         # Append a space if the type is not a pointer
60         return (type ~ /\*$/) ? type : type " ";
61 }
62
63 # These are just for convenience ...
64 function printc(s) {print s > cfile;}
65 function printh(s) {print s > hfile;}
66
67 function add_debug_code(name, arg, pos)
68 {
69         if (arg == "vpp")
70                 arg = "*vpp";
71         if (lockdata[name, arg, pos] && (lockdata[name, arg, pos] != "-")) {
72                 if (arg ~ /^\*/) {
73                         printh("\tif ("substr(arg, 2)" != NULL) {");
74                 }
75                 # Add assertions for locking
76                 if (lockdata[name, arg, pos] == "L")
77                         printh("\tASSERT_VOP_LOCKED("arg", \""uname"\");");
78                 else if (lockdata[name, arg, pos] == "U")
79                         printh("\tASSERT_VOP_UNLOCKED("arg", \""uname"\");");
80                 else if (0) {
81                         # XXX More checks!
82                 }
83                 if (arg ~ /^\*/) {
84                         printh("\t}");
85                 }
86         }
87 }
88
89 function add_debug_pre(name)
90 {
91         if (lockdata[name, "pre"]) {
92                 printh("#ifdef  DEBUG_VFS_LOCKS");
93                 printh("\t"lockdata[name, "pre"]"(&a);");
94                 printh("#endif");
95         }
96 }
97
98 function add_debug_post(name)
99 {
100         if (lockdata[name, "post"]) {
101                 printh("#ifdef  DEBUG_VFS_LOCKS");
102                 printh("\t"lockdata[name, "post"]"(&a, rc);");
103                 printh("#endif");
104         }
105 }
106
107 function find_arg_with_type (type)
108 {
109         for (jj = 0; jj < numargs; jj++) {
110                 if (types[jj] == type) {
111                         return "VOPARG_OFFSETOF(struct " \
112                             name "_args,a_" args[jj] ")";
113                 }
114         }
115
116         return "VDESC_NO_OFFSET";
117 }
118
119 BEGIN{
120
121 # Process the command line
122 for (i = 1; i < ARGC; i++) {
123         arg = ARGV[i];
124         if (arg !~ /^-[ch]+$/ && arg !~ /\.src$/)
125                 usage();
126         if (arg ~ /^-.*c/)
127                 cfile = "vnode_if.c";
128         if (arg ~ /^-.*h/)
129                 hfile = "vnode_if.h";
130         if (arg ~ /\.src$/)
131                 srcfile = arg;
132 }
133 ARGC = 1;
134
135 if (!cfile && !hfile)
136         exit 0;
137
138 if (!srcfile)
139         usage();
140
141 common_head = \
142     "/*\n" \
143     " * This file is produced automatically.\n" \
144     " * Do not modify anything in here by hand.\n" \
145     " *\n" \
146     " * Created from $DragonFly: src/sys/tools/Attic/vnode_if.awk,v 1.4 2003/11/22 21:12:35 asmodai Exp $\n" \
147     " */\n" \
148     "\n";
149
150 if (hfile)
151         printh(common_head "extern struct vnodeop_desc vop_default_desc;");
152
153 if (cfile) {
154         printc(common_head \
155             "#include <sys/param.h>\n" \
156             "#include <sys/systm.h>\n" \
157             "#include <sys/vnode.h>\n" \
158             "\n" \
159             "struct vnodeop_desc vop_default_desc = {\n" \
160             "   1,\t\t\t/* special case, vop_default => 1 */\n" \
161             "   \"default\",\n" \
162             "   0,\n" \
163             "   NULL,\n" \
164             "   VDESC_NO_OFFSET,\n" \
165             "   VDESC_NO_OFFSET,\n" \
166             "   VDESC_NO_OFFSET,\n" \
167             "   VDESC_NO_OFFSET,\n" \
168             "   NULL,\n" \
169             "};\n");
170 }
171
172 while ((getline < srcfile) > 0) {
173         if (NF == 0)
174                 continue;
175         if ($1 ~ /^#%/) {
176                 if (NF != 6  ||  $1 != "#%"  || \
177                     $2 !~ /^[a-z]+$/  ||  $3 !~ /^[a-z]+$/  || \
178                     $4 !~ /^.$/  ||  $5 !~ /^.$/  ||  $6 !~ /^.$/)
179                         continue;
180                 if ($3 == "vpp")
181                         $3 = "*vpp";
182                 lockdata["vop_" $2, $3, "Entry"] = $4;
183                 lockdata["vop_" $2, $3, "OK"]    = $5;
184                 lockdata["vop_" $2, $3, "Error"] = $6;                  
185                 continue;
186         }
187
188         if ($1 ~ /^#!/) {
189                 if (NF != 4 || $1 != "#!")
190                         continue;
191                 if ($3 != "pre" && $3 != "post")
192                         continue;
193                 lockdata["vop_" $2, $3] = $4;
194                 continue;
195         }
196         if ($1 ~ /^#/)
197                 continue;
198
199         # Get the function name.
200         name = $1;
201         uname = toupper(name);
202
203         # Get the function arguments.
204         for (numargs = 0; ; ++numargs) {
205                 if ((getline < srcfile) <= 0) {
206                         die("Unable to read through the arguments for \"%s\"",
207                             name);
208                 }
209                 if ($1 ~ /^\};/)
210                         break;
211
212                 # Delete comments, if any.
213                 gsub (/\/\*.*\*\//, "");
214
215                 # Condense whitespace and delete leading/trailing space.
216                 gsub(/[[:space:]]+/, " ");
217                 sub(/^ /, "");
218                 sub(/ $/, "");
219
220                 # Pick off direction.
221                 if ($1 != "INOUT" && $1 != "IN" && $1 != "OUT")
222                         die("No IN/OUT direction for \"%s\".", $0);
223                 dirs[numargs] = $1;
224                 sub(/^[A-Z]* /, "");
225
226                 if ((reles[numargs] = $1) == "WILLRELE")
227                         sub(/^[A-Z]* /, "");
228                 else
229                         reles[numargs] = "WONTRELE";
230
231                 # kill trailing ;
232                 if (sub(/;$/, "") < 1)
233                         die("Missing end-of-line ; in \"%s\".", $0);
234
235                 # pick off variable name
236                 if ((argp = match($0, /[A-Za-z0-9_]+$/)) < 1)
237                         die("Missing var name \"a_foo\" in \"%s\".", $0);
238                 args[numargs] = substr($0, argp);
239                 $0 = substr($0, 1, argp - 1);
240
241                 # what is left must be type
242                 # remove trailing space (if any)
243                 sub(/ $/, "");
244                 types[numargs] = $0;
245
246         }
247
248         if (hfile) {
249                 # Print out the vop_F_args structure.
250                 printh("struct "name"_args {\n\tstruct vnodeop_desc *a_desc;");
251                 for (i = 0; i < numargs; ++i)
252                         printh("\t" t_spc(types[i]) "a_" args[i] ";");
253                 printh("};");
254
255                 # Print out extern declaration.
256                 printh("extern struct vnodeop_desc " name "_desc;");
257
258                 # Print out function.
259                 printh("static __inline int " uname "(");
260                 for (i = 0; i < numargs; ++i) {
261                         printh("\t" t_spc(types[i]) args[i] \
262                             (i < numargs - 1 ? "," : ")"));
263                 }
264                 printh("{\n\tstruct " name "_args a;");
265                 printh("\tint rc;");
266                 printh("\ta.a_desc = VDESC(" name ");");
267                 for (i = 0; i < numargs; ++i)
268                         printh("\ta.a_" args[i] " = " args[i] ";");
269                 for (i = 0; i < numargs; ++i)
270                         add_debug_code(name, args[i], "Entry");
271                 add_debug_pre(name);
272                 printh("\trc = VCALL(" args[0] ", VOFFSET(" name "), &a);");
273                 printh("if (rc == 0) {");
274                 for (i = 0; i < numargs; ++i)
275                         add_debug_code(name, args[i], "OK");
276                 printh("} else {");
277                 for (i = 0; i < numargs; ++i)
278                         add_debug_code(name, args[i], "Error");
279                 printh("}");
280                 add_debug_post(name);
281                 printh("\treturn (rc);\n}");
282         }
283
284         if (cfile) {
285                 # Print out the vop_F_vp_offsets structure.  This all depends
286                 # on naming conventions and nothing else.
287                 printc("static int " name "_vp_offsets[] = {");
288                 # as a side effect, figure out the releflags
289                 releflags = "";
290                 vpnum = 0;
291                 for (i = 0; i < numargs; i++) {
292                         if (types[i] == "struct vnode *") {
293                                 printc("\tVOPARG_OFFSETOF(struct " name \
294                                     "_args,a_" args[i] "),");
295                                 if (reles[i] == "WILLRELE") {
296                                         releflags = releflags \
297                                             "|VDESC_VP" vpnum "_WILLRELE";
298                                 }
299                                 vpnum++;
300                         }
301                 }
302
303                 sub(/^\|/, "", releflags);
304                 printc("\tVDESC_NO_OFFSET");
305                 printc("};");
306
307                 # Print out the vnodeop_desc structure.
308                 printc("struct vnodeop_desc " name "_desc = {");
309                 # offset
310                 printc("\t0,");
311                 # printable name
312                 printc("\t\"" name "\",");
313                 # flags
314                 vppwillrele = "";
315                 for (i = 0; i < numargs; i++) {
316                         if (types[i] == "struct vnode **" && \
317                             reles[i] == "WILLRELE") {
318                                 vppwillrele = "|VDESC_VPP_WILLRELE";
319                         }
320                 }
321
322                 if (!releflags)
323                         releflags = "0";
324                 printc("\t" releflags vppwillrele ",");
325
326                 # vp offsets
327                 printc("\t" name "_vp_offsets,");
328                 # vpp (if any)
329                 printc("\t" find_arg_with_type("struct vnode **") ",");
330                 # cred (if any)
331                 printc("\t" find_arg_with_type("struct ucred *") ",");
332                 # thread (if any)
333                 printc("\t" find_arg_with_type("struct thread *") ",");
334                 # componentname
335                 printc("\t" find_arg_with_type("struct componentname *") ",");
336                 # transport layer information
337                 printc("\tNULL,\n};\n");
338         }
339 }
340  
341 if (hfile)
342         close(hfile);
343 if (cfile)
344         close(cfile);
345 close(srcfile);
346
347 exit 0;
348
349 }