makerumpif.sh revision 1.7 1 1.1 pooka #!/bin/sh
2 1.1 pooka #
3 1.7 pooka # $NetBSD: makerumpif.sh,v 1.7 2014/04/25 13:07:31 pooka Exp $
4 1.1 pooka #
5 1.1 pooka # Copyright (c) 2009 Antti Kantee. All rights reserved.
6 1.1 pooka #
7 1.1 pooka # Redistribution and use in source and binary forms, with or without
8 1.1 pooka # modification, are permitted provided that the following conditions
9 1.1 pooka # are met:
10 1.1 pooka # 1. Redistributions of source code must retain the above copyright
11 1.1 pooka # notice, this list of conditions and the following disclaimer.
12 1.1 pooka # 2. Redistributions in binary form must reproduce the above copyright
13 1.1 pooka # notice, this list of conditions and the following disclaimer in the
14 1.1 pooka # documentation and/or other materials provided with the distribution.
15 1.1 pooka #
16 1.1 pooka # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 pooka # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 pooka # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 pooka # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 pooka # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 pooka # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 pooka # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 pooka # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 pooka # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 pooka # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 pooka #
27 1.1 pooka
28 1.1 pooka #
29 1.1 pooka # This reads a rump component interface description and creates:
30 1.1 pooka # 1: rump private prototypes for internal calls
31 1.1 pooka # 2: public prototypes for calls outside of rump
32 1.1 pooka # 3: public interface implementations which run the rump scheduler
33 1.1 pooka # and call the private interface
34 1.1 pooka #
35 1.1 pooka
36 1.1 pooka usage ()
37 1.1 pooka {
38 1.1 pooka
39 1.1 pooka echo "usage: $0 spec"
40 1.1 pooka exit 1
41 1.1 pooka }
42 1.1 pooka
43 1.1 pooka boom ()
44 1.1 pooka {
45 1.1 pooka
46 1.1 pooka echo $*
47 1.1 pooka exit 1
48 1.1 pooka }
49 1.1 pooka
50 1.6 pooka unset TOPDIR
51 1.6 pooka if [ $# -eq 3 ]; then
52 1.6 pooka if [ $1 = '-R' ]; then
53 1.6 pooka TOPDIR=$2
54 1.6 pooka else
55 1.6 pooka usage
56 1.6 pooka fi
57 1.6 pooka shift; shift
58 1.6 pooka fi
59 1.6 pooka [ $# -ne 1 ] && usage
60 1.1 pooka
61 1.1 pooka MYDIR=`pwd`
62 1.6 pooka if [ -z "${TOPDIR}" ]; then
63 1.6 pooka while [ ! -f Makefile.rump ]; do
64 1.6 pooka [ `pwd` = '/' ] && boom Could not find rump topdir.
65 1.6 pooka cd ..
66 1.6 pooka done
67 1.6 pooka TOPDIR="`pwd`"
68 1.6 pooka fi
69 1.1 pooka cd ${MYDIR}
70 1.1 pooka
71 1.1 pooka sed -e '
72 1.1 pooka :again
73 1.1 pooka /\\$/{
74 1.1 pooka N
75 1.1 pooka s/[ ]*\\\n[ ]*/ /
76 1.1 pooka b again
77 1.1 pooka }
78 1.6 pooka ' ${1} | awk -F\| -v topdir=${TOPDIR} '
79 1.1 pooka function fileheaders(file, srcstr)
80 1.1 pooka {
81 1.7 pooka printf("/*\t$NetBSD: makerumpif.sh,v 1.7 2014/04/25 13:07:31 pooka Exp $\t*/\n\n") > file
82 1.1 pooka printf("/*\n * Automatically generated. DO NOT EDIT.\n") > file
83 1.7 pooka genstr = "$NetBSD: makerumpif.sh,v 1.7 2014/04/25 13:07:31 pooka Exp $"
84 1.1 pooka gsub("\\$", "", genstr)
85 1.1 pooka printf(" * from: %s\n", srcstr) > file
86 1.1 pooka printf(" * by: %s\n", genstr) > file
87 1.1 pooka printf(" */\n") > file
88 1.1 pooka }
89 1.1 pooka
90 1.1 pooka function die(str)
91 1.1 pooka {
92 1.1 pooka
93 1.1 pooka print str
94 1.1 pooka exit(1)
95 1.1 pooka }
96 1.1 pooka
97 1.1 pooka NR == 1 {
98 1.1 pooka sub(";[^\\$]*\\$", "")
99 1.1 pooka sub("\\$", "")
100 1.1 pooka fromstr = $0
101 1.1 pooka next
102 1.1 pooka }
103 1.1 pooka
104 1.1 pooka $1 == "NAME"{myname = $2;next}
105 1.6 pooka $1 == "PUBHDR"{pubhdr = topdir "/" $2;print pubhdr;next}
106 1.6 pooka $1 == "PRIVHDR"{privhdr = topdir "/" $2;print privhdr;next}
107 1.6 pooka $1 == "WRAPPERS"{gencalls = topdir "/" $2;print gencalls;next}
108 1.1 pooka
109 1.1 pooka /^;/{next}
110 1.1 pooka /\\$/{sub("\\\n", "");getline nextline;$0 = $0 nextline}
111 1.1 pooka /^[ \t]*$/{next}
112 1.1 pooka {
113 1.1 pooka if (NF != 3 && NF != 4) {
114 1.1 pooka die("error: unexpected number of fields\n")
115 1.1 pooka }
116 1.1 pooka if (NF == 4) {
117 1.1 pooka if ($4 == "WEAK")
118 1.1 pooka isweak = 1
119 1.1 pooka else
120 1.1 pooka die("error: unexpected fourth field");
121 1.1 pooka } else {
122 1.1 pooka isweak = 0
123 1.1 pooka }
124 1.1 pooka if (!myname)
125 1.1 pooka die("name not specified");
126 1.1 pooka if (!pubhdr)
127 1.1 pooka die("public header not specified");
128 1.1 pooka if (!privhdr)
129 1.1 pooka die("private header not specified");
130 1.1 pooka if (!gencalls)
131 1.1 pooka die("wrapper file not specified");
132 1.1 pooka
133 1.1 pooka if (!once) {
134 1.1 pooka fileheaders(pubhdr, fromstr)
135 1.1 pooka fileheaders(privhdr, fromstr)
136 1.1 pooka fileheaders(gencalls, fromstr)
137 1.1 pooka once = 1
138 1.1 pooka
139 1.1 pooka pubfile = pubhdr
140 1.1 pooka sub(".*/", "", pubfile)
141 1.1 pooka
142 1.1 pooka privfile = privhdr
143 1.1 pooka sub(".*/", "", privfile)
144 1.1 pooka
145 1.1 pooka printf("\n") > pubhdr
146 1.1 pooka printf("\n") > privhdr
147 1.1 pooka
148 1.1 pooka printf("\n#include <sys/cdefs.h>\n") > gencalls
149 1.1 pooka printf("#include <sys/systm.h>\n") > gencalls
150 1.1 pooka printf("\n#include <rump/rump.h>\n") > gencalls
151 1.1 pooka printf("#include <rump/%s>\n\n", pubfile) > gencalls
152 1.4 pooka printf("#include \"rump_private.h\"\n", privfile) > gencalls
153 1.1 pooka printf("#include \"%s\"\n\n", privfile) > gencalls
154 1.2 pooka printf("void __dead rump_%s_unavailable(void);\n", \
155 1.1 pooka myname) > gencalls
156 1.1 pooka printf("void __dead\nrump_%s_unavailable(void)\n{\n", \
157 1.1 pooka myname) > gencalls
158 1.1 pooka printf("\n\tpanic(\"%s interface unavailable\");\n}\n", \
159 1.1 pooka myname) > gencalls
160 1.1 pooka }
161 1.1 pooka
162 1.1 pooka funtype = $1
163 1.1 pooka sub("[ \t]*$", "", funtype)
164 1.1 pooka funname = $2
165 1.1 pooka sub("[ \t]*$", "", funname)
166 1.1 pooka funargs = $3
167 1.2 pooka sub("[ \t]*$", "", funargs)
168 1.2 pooka
169 1.3 pooka printf("%s rump_pub_%s(%s);\n", funtype, funname, funargs) > pubhdr
170 1.3 pooka printf("%s rump_%s(%s);\n", funtype, funname, funargs) > privhdr
171 1.7 pooka printf("typedef %s (*rump_%s_fn)(%s);\n", \
172 1.7 pooka funtype, funname, funargs) > privhdr
173 1.1 pooka
174 1.1 pooka if (funtype == "void")
175 1.1 pooka voidret = 1
176 1.1 pooka else
177 1.1 pooka voidret = 0
178 1.1 pooka if (funargs == "void")
179 1.1 pooka voidarg = 1
180 1.1 pooka else
181 1.1 pooka voidarg = 0
182 1.1 pooka
183 1.3 pooka printf("\n%s\nrump_pub_%s(", funtype, funname) > gencalls
184 1.1 pooka if (!voidarg) {
185 1.1 pooka narg = split(funargs, argv, ",")
186 1.1 pooka for (i = 1; i <= narg; i++) {
187 1.1 pooka sub(" *", "", argv[i])
188 1.1 pooka if (match(argv[i], "\\*$") != 0)
189 1.1 pooka printf("%sarg%d", argv[i], i) > gencalls
190 1.1 pooka else
191 1.1 pooka printf("%s arg%d", argv[i], i) > gencalls
192 1.1 pooka if (i != narg)
193 1.1 pooka printf(", ") > gencalls
194 1.1 pooka }
195 1.1 pooka } else {
196 1.1 pooka narg = 0
197 1.1 pooka printf("void") > gencalls
198 1.1 pooka }
199 1.1 pooka printf(")\n{\n") > gencalls
200 1.1 pooka
201 1.1 pooka if (!voidret) {
202 1.2 pooka printf("\t%s rv;\n", funtype) > gencalls
203 1.1 pooka }
204 1.4 pooka printf("\n\trump_schedule();\n\t") > gencalls
205 1.1 pooka if (!voidret)
206 1.1 pooka printf("rv = ") > gencalls
207 1.3 pooka printf("rump_%s(", funname) > gencalls
208 1.1 pooka for (i = 1; i <= narg; i++) {
209 1.1 pooka printf("arg%i", i) > gencalls
210 1.1 pooka if (i < narg)
211 1.1 pooka printf(", ") > gencalls
212 1.1 pooka }
213 1.4 pooka printf(");\n\trump_unschedule();\n") > gencalls
214 1.1 pooka if (!voidret)
215 1.1 pooka printf("\n\treturn rv;\n") > gencalls
216 1.1 pooka printf("}\n") > gencalls
217 1.1 pooka if (isweak)
218 1.3 pooka printf("__weak_alias(rump_%s,rump_%s_unavailable);\n", \
219 1.1 pooka funname, myname) > gencalls
220 1.1 pooka }'
221