mknodes.sh revision 1.3
1#! /bin/sh 2# $NetBSD: mknodes.sh,v 1.3 2018/06/22 11:04:55 kre Exp $ 3 4# Copyright (c) 2003 The NetBSD Foundation, Inc. 5# All rights reserved. 6# 7# This code is derived from software contributed to The NetBSD Foundation 8# by David Laight. 9# 10# Redistribution and use in source and binary forms, with or without 11# modification, are permitted provided that the following conditions 12# are met: 13# 1. Redistributions of source code must retain the above copyright 14# notice, this list of conditions and the following disclaimer. 15# 2. Redistributions in binary form must reproduce the above copyright 16# notice, this list of conditions and the following disclaimer in the 17# documentation and/or other materials provided with the distribution. 18# 19# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29# POSSIBILITY OF SUCH DAMAGE. 30 31nodetypes=$1 32nodes_pat=$2 33objdir="$3" 34 35exec <$nodetypes 36exec >$objdir/nodes.h.tmp 37 38echo "/*" 39echo " * This file was generated by mknodes.sh" 40echo " */" 41echo 42 43tagno=0 44while IFS=; read -r line; do 45 line="${line%%#*}" 46 IFS=' ' 47 set -- $line 48 IFS= 49 [ -z "$2" ] && continue 50 case "$line" in 51 [" "]* ) 52 IFS=' ' 53 [ $field = 0 ] && struct_list="$struct_list $struct" 54 eval field_${struct}_$field=\"\$*\" 55 eval numfld_$struct=\$field 56 field=$(($field + 1)) 57 ;; 58 * ) 59 define=$1 60 struct=$2 61 echo "#define $define $tagno" 62 tagno=$(($tagno + 1)) 63 eval define_$struct=\"\$define_$struct \$define\" 64 struct_define="$struct_define $struct" 65 field=0 66 ;; 67 esac 68done 69 70echo 71 72IFS=' ' 73for struct in $struct_list; do 74 echo 75 echo 76 echo "struct $struct {" 77 field=0 78 while 79 eval line=\"\$field_${struct}_$field\" 80 field=$(($field + 1)) 81 [ -n "$line" ] 82 do 83 IFS=' ' 84 set -- $line 85 name=$1 86 case $2 in 87 nodeptr ) type="union node *";; 88 nodelist ) type="struct nodelist *";; 89 string ) type="char *";; 90 int ) type="int ";; 91 * ) name=; shift 2; type="$*";; 92 esac 93 echo " $type$name;" 94 done 95 echo "};" 96done 97 98echo 99echo 100echo "union node {" 101echo " int type;" 102for struct in $struct_list; do 103 echo " struct $struct $struct;" 104done 105echo "};" 106echo 107echo 108echo "struct nodelist {" 109echo " struct nodelist *next;" 110echo " union node *n;" 111echo "};" 112echo 113echo 114echo 'struct funcdef;' 115echo 'struct funcdef *copyfunc(union node *);' 116echo 'union node *getfuncnode(struct funcdef *);' 117echo 'void reffunc(struct funcdef *);' 118echo 'void unreffunc(struct funcdef *);' 119echo 'void freefunc(struct funcdef *);' 120 121mv $objdir/nodes.h.tmp $objdir/nodes.h || exit 1 122 123exec <$nodes_pat 124exec >$objdir/nodes.c.tmp 125 126echo "/*" 127echo " * This file was generated by mknodes.sh" 128echo " */" 129echo 130 131while IFS=; read -r line; do 132 IFS=' ' 133 set -- $line 134 IFS= 135 case "$1" in 136 '%SIZES' ) 137 echo "static const short nodesize[$tagno] = {" 138 IFS=' ' 139 for struct in $struct_define; do 140 echo " SHELL_ALIGN(sizeof (struct $struct))," 141 done 142 echo "};" 143 ;; 144 '%CALCSIZE' ) 145 echo " if (n == NULL)" 146 echo " return;" 147 echo " res->bsize += nodesize[n->type];" 148 echo " switch (n->type) {" 149 IFS=' ' 150 for struct in $struct_list; do 151 eval defines=\"\$define_$struct\" 152 for define in $defines; do 153 echo " case $define:" 154 done 155 eval field=\$numfld_$struct 156 while 157 [ $field != 0 ] 158 do 159 eval line=\"\$field_${struct}_$field\" 160 field=$(($field - 1)) 161 IFS=' ' 162 set -- $line 163 name=$1 164 cl=", res)" 165 case $2 in 166 nodeptr ) fn=calcsize;; 167 nodelist ) fn=sizenodelist;; 168 string ) fn="res->ssize += strlen" 169 cl=") + 1";; 170 * ) continue;; 171 esac 172 echo " ${fn}(n->$struct.$name${cl};" 173 done 174 echo " break;" 175 done 176 echo " };" 177 ;; 178 '%COPY' ) 179 echo " if (n == NULL)" 180 echo " return NULL;" 181 echo " new = st->block;" 182 echo " st->block = (char *) st->block + nodesize[n->type];" 183 echo " switch (n->type) {" 184 IFS=' ' 185 for struct in $struct_list; do 186 eval defines=\"\$define_$struct\" 187 for define in $defines; do 188 echo " case $define:" 189 done 190 eval field=\$numfld_$struct 191 while 192 [ $field != 0 ] 193 do 194 eval line=\"\$field_${struct}_$field\" 195 field=$(($field - 1)) 196 IFS=' ' 197 set -- $line 198 name=$1 199 case $2 in 200 nodeptr ) fn="copynode(";; 201 nodelist ) fn="copynodelist(";; 202 string ) fn="nodesavestr(";; 203 int ) fn=;; 204 * ) continue;; 205 esac 206 f="$struct.$name" 207 echo " new->$f = ${fn}n->$f${fn:+, st)};" 208 done 209 echo " break;" 210 done 211 echo " };" 212 echo " new->type = n->type;" 213 ;; 214 * ) echo "$line";; 215 esac 216done 217 218mv $objdir/nodes.c.tmp $objdir/nodes.c || exit 1 219