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