Home | History | Annotate | Line # | Download | only in librump
      1 #!/bin/sh
      2 #
      3 #	$NetBSD: makerumpif.sh,v 1.10 2016/01/26 23:21:18 pooka Exp $
      4 #
      5 # Copyright (c) 2009, 2015 Antti Kantee.  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 #
     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.
     26 #
     27 
     28 #
     29 # This reads a rump component interface description and creates:
     30 #  1: rump private prototypes for internal calls
     31 #  2: public prototypes for calls outside of rump
     32 #  3: public interface implementations which run the rump scheduler
     33 #     and call the private interface
     34 #
     35 
     36 usage ()
     37 {
     38 
     39 	echo "usage: $0 spec"
     40 	exit 1
     41 }
     42 
     43 boom ()
     44 {
     45 
     46 	echo $*
     47 	exit 1
     48 }
     49 
     50 unset TOPDIR
     51 if [ $# -eq 3 ]; then
     52 	if [ $1 = '-R' ]; then
     53 		TOPDIR=$2
     54 	else
     55 		usage
     56 	fi
     57 	shift; shift
     58 fi
     59 [ $# -ne 1 ] && usage
     60 
     61 MYDIR=`pwd`
     62 if [ -z "${TOPDIR}" ]; then
     63 	while [ ! -f Makefile.rump  ]; do
     64 		[ `pwd` = '/' ] && boom Could not find rump topdir.
     65 		cd ..
     66 	done
     67 	TOPDIR="`pwd`"
     68 fi
     69 cd ${MYDIR}
     70 
     71 sed -e '
     72 :again
     73 	/\\$/{
     74 		N
     75 		s/[ 	]*\\\n[ 	]*/ /
     76 		b again
     77 	}
     78 ' ${1} | awk -F\| -v topdir=${TOPDIR} '
     79 function fileheaders(file, srcstr)
     80 {
     81 	printf("/*\t$NetBSD: makerumpif.sh,v 1.10 2016/01/26 23:21:18 pooka Exp $\t*/\n\n") > file
     82 	printf("/*\n * Automatically generated.  DO NOT EDIT.\n") > file
     83 	genstr = "$NetBSD: makerumpif.sh,v 1.10 2016/01/26 23:21:18 pooka Exp $"
     84 	gsub("\\$", "", genstr)
     85 	printf(" * from: %s\n", srcstr) > file
     86 	printf(" * by:   %s\n", genstr) > file
     87 	printf(" */\n") > file
     88 }
     89 
     90 function die(str)
     91 {
     92 
     93 	print str
     94 	exit(1)
     95 }
     96 
     97 NR == 1 {
     98 	sub(";[^\\$]*\\$", "")
     99 	sub("\\$", "")
    100 	fromstr = $0
    101 	next
    102 }
    103 
    104 $1 == "NAME"{myname = $2;next}
    105 $1 == "PUBHDR"{pubhdr = topdir "/" $2;print pubhdr;next}
    106 $1 == "PRIVHDR"{privhdr = topdir "/" $2;print privhdr;next}
    107 $1 == "WRAPPERS"{gencalls = topdir "/" $2;print gencalls;next}
    108 
    109 /^;/{next}
    110 /\\$/{sub("\\\n", "");getline nextline;$0 = $0 nextline}
    111 /^[ \t]*$/{next}
    112 {
    113 	if (NF != 3 && NF != 4) {
    114 		die("error: unexpected number of fields\n")
    115 	}
    116 	isweak = 0
    117 	iscompat = 0
    118 	if (NF == 4) {
    119 		if ($4 == "WEAK") {
    120 			isweak = 1
    121 		} else if (match($4, "COMPAT_")) {
    122 			iscompat = 1
    123 			compat = $4
    124 		} else {
    125 			die("error: unexpected fourth field");
    126 		}
    127 	}
    128 	if (!myname)
    129 		die("name not specified");
    130 	if (!pubhdr)
    131 		die("public header not specified");
    132 	if (!privhdr)
    133 		die("private header not specified");
    134 	if (!gencalls)
    135 		die("wrapper file not specified");
    136 
    137 	if (!once) {
    138 		fileheaders(pubhdr, fromstr)
    139 		fileheaders(privhdr, fromstr)
    140 		fileheaders(gencalls, fromstr)
    141 		once = 1
    142 
    143 		pubfile = pubhdr
    144 		sub(".*/", "", pubfile)
    145 
    146 		privfile = privhdr
    147 		sub(".*/", "", privfile)
    148 
    149 		printf("\n") > pubhdr
    150 		printf("\n") > privhdr
    151 
    152 		printf("#ifndef _RUMP_PRIF_%s_H_\n", toupper(myname)) > privhdr
    153 		printf("#define _RUMP_PRIF_%s_H_\n", toupper(myname)) > privhdr
    154 		printf("\n") > privhdr
    155 
    156 		printf("\n#include <sys/cdefs.h>\n") > gencalls
    157 		printf("#include <sys/systm.h>\n\n") > gencalls
    158 		printf("#include <rump-sys/kern.h>\n", privfile) > gencalls
    159 		printf("#include <rump-sys/%s>\n\n", privfile) > gencalls
    160 		printf("#include <rump/rump.h>\n") > gencalls
    161 		printf("#include <rump/%s>\n\n", pubfile) > gencalls
    162 		printf("void __dead rump_%s_unavailable(void);\n",	\
    163 		    myname) > gencalls
    164 		printf("void __dead\nrump_%s_unavailable(void)\n{\n",	\
    165 		    myname) > gencalls
    166 		printf("\n\tpanic(\"%s interface unavailable\");\n}\n",	\
    167 		    myname) > gencalls
    168 	}
    169 
    170 	funtype = $1
    171 	sub("[ \t]*$", "", funtype)
    172 	funname = $2
    173 	sub("[ \t]*$", "", funname)
    174 	funargs = $3
    175 	sub("[ \t]*$", "", funargs)
    176 
    177 	printf("%s rump_pub_%s(%s);\n", funtype, funname, funargs) > pubhdr
    178 	printf("%s rump_%s(%s);\n", funtype, funname, funargs) > privhdr
    179 	printf("typedef %s (*rump_%s_fn)(%s);\n",	\
    180 	    funtype, funname, funargs) > privhdr
    181 
    182 	if (funtype == "void")
    183 		voidret = 1
    184 	else
    185 		voidret = 0
    186 	if (funargs == "void")
    187 		voidarg = 1
    188 	else
    189 		voidarg = 0
    190 
    191 	printf("\n") > gencalls
    192 	if (iscompat) {
    193 		printf("#ifdef %s\n", compat) > gencalls
    194 	}
    195 
    196 	printf("%s\nrump_pub_%s(", funtype, funname) > gencalls
    197 	if (!voidarg) {
    198 		narg = split(funargs, argv, ",")
    199 		for (i = 1; i <= narg; i++) {
    200 			sub(" *", "", argv[i])
    201 			if (match(argv[i], "\\*$") != 0)
    202 				printf("%sarg%d", argv[i], i) > gencalls
    203 			else
    204 				printf("%s arg%d", argv[i], i) > gencalls
    205 			if (i != narg)
    206 				printf(", ") > gencalls
    207 		}
    208 	} else {
    209 		narg = 0
    210 		printf("void") > gencalls
    211 	}
    212 	printf(")\n{\n") > gencalls
    213 
    214 	if (!voidret) {
    215 		printf("\t%s rv;\n", funtype) > gencalls
    216 	}
    217 	printf("\n\trump_schedule();\n\t") > gencalls
    218 	if (!voidret)
    219 		printf("rv = ") > gencalls
    220 	printf("rump_%s(", funname) > gencalls
    221 	for (i = 1; i <= narg; i++) {
    222 		printf("arg%i", i) > gencalls
    223 		if (i < narg)
    224 			printf(", ") > gencalls
    225 	}
    226 	printf(");\n\trump_unschedule();\n") > gencalls
    227 	if (!voidret)
    228 		printf("\n\treturn rv;\n") > gencalls
    229 	printf("}\n") > gencalls
    230 	if (iscompat) {
    231 		printf("#else\n") > gencalls
    232 		printf("__strong_alias(rump_pub_%s,rump_%s_unavailable);\n", \
    233 		    funname, myname) > gencalls
    234 		printf("#endif /* %s */\n", compat) > gencalls
    235 	}
    236 	if (isweak)
    237 		printf("__weak_alias(rump_%s,rump_%s_unavailable);\n", \
    238 		    funname, myname) > gencalls
    239 }
    240 
    241 END { printf("\n#endif /* _RUMP_PRIF_%s_H_ */\n", toupper(myname)) > privhdr }'
    242