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