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