1#! /bin/sh
2# Generate CGEN simulator files.
3#
4# Usage: /bin/sh cgen.sh {"arch"|"cpu"|"decode"|"defs"|"cpu-decode"} \
5#	srcdir cgen cgendir cgenflags \
6#	arch archflags cpu mach suffix archfile extrafiles opcfile
7#
8# We store the generated files in the source directory until we decide to
9# ship a Scheme interpreter (or other implementation) with gdb/binutils.
10# Maybe we never will.
11
12# We want to behave like make, any error forces us to stop.
13set -e
14
15action=$1
16srcdir=$2
17cgen="$3"
18cgendir=$4
19cgenflags=$5
20arch=$6
21archflags=$7
22cpu=$8
23isa=$9
24# portably bring parameters beyond $9 into view
25shift ; mach=$9
26shift ; suffix=$9
27shift ; archfile=$9
28shift ; extrafiles=$9
29shift ; opcfile=$9
30
31rootdir=${srcdir}/../..
32move_if_change="${CONFIG_SHELL:-/bin/sh} ${rootdir}/move-if-change"
33
34test -z "${opcfile}" && opcfile=/dev/null
35
36if test -z "$isa" ; then
37  isa=all
38  prefix=$cpu
39else
40  prefix=${cpu}_$isa
41fi
42
43lowercase='abcdefghijklmnopqrstuvwxyz'
44uppercase='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
45ARCH=`echo ${arch} | tr "${lowercase}" "${uppercase}"`
46CPU=`echo ${cpu} | tr "${lowercase}" "${uppercase}"`
47PREFIX=`echo ${prefix} | tr "${lowercase}" "${uppercase}"`
48
49sedscript="\
50-e s/@ARCH@/${ARCH}/g -e s/@arch@/${arch}/g \
51-e s/@CPU@/${CPU}/g -e s/@cpu@/${cpu}/g \
52-e s/@PREFIX@/${PREFIX}/g -e s/@prefix@/${prefix}/g"
53
54# avoid collisions in parallel makes
55tmp=tmp-$$
56
57case $action in
58arch)
59	rm -f ${tmp}-arch.h1 ${tmp}-arch.h
60	rm -f ${tmp}-arch.c1 ${tmp}-arch.c
61	rm -f ${tmp}-all.h1 ${tmp}-all.h
62
63	${cgen} ${cgendir}/cgen-sim.scm \
64		-s ${cgendir} \
65		${cgenflags} \
66		-f "${archflags}" \
67		-m ${mach} \
68		-a ${archfile} \
69		-i ${isa} \
70		-A ${tmp}-arch.h1 \
71		-B ${tmp}-arch.c1 \
72		-N ${tmp}-all.h1
73	sed $sedscript < ${tmp}-arch.h1 > ${tmp}-arch.h
74	${move_if_change} ${tmp}-arch.h ${srcdir}/arch.h
75	sed $sedscript < ${tmp}-arch.c1 > ${tmp}-arch.c
76	${move_if_change} ${tmp}-arch.c ${srcdir}/arch.c
77	sed $sedscript < ${tmp}-all.h1 > ${tmp}-all.h
78	${move_if_change} ${tmp}-all.h ${srcdir}/cpuall.h
79
80	rm -f ${tmp}-arch.h1 ${tmp}-arch.c1 ${tmp}-all.h1
81	;;
82
83cpu | decode | cpu-decode)
84
85	fileopts=""
86
87	case $action in
88	*cpu*)
89		rm -f ${tmp}-cpu.h1 ${tmp}-cpu.c1
90		rm -f ${tmp}-ext.c1 ${tmp}-read.c1 ${tmp}-write.c1
91		rm -f ${tmp}-sem.c1 ${tmp}-semsw.c1
92		rm -f ${tmp}-mod.c1
93		rm -f ${tmp}-cpu.h ${tmp}-cpu.c
94		rm -f ${tmp}-ext.c ${tmp}-read.c ${tmp}-write.c
95		rm -f ${tmp}-sem.c ${tmp}-semsw.c ${tmp}-mod.c
96		fileopts="$fileopts \
97			-C ${tmp}-cpu.h1 \
98			-U ${tmp}-cpu.c1 \
99			-M ${tmp}-mod.c1"
100		;;
101	esac
102
103	case $action in
104	*decode*)
105		rm -f ${tmp}-dec.h1 ${tmp}-dec.h ${tmp}-dec.c1 ${tmp}-dec.c
106		fileopts="$fileopts \
107			-T ${tmp}-dec.h1 \
108			-D ${tmp}-dec.c1"
109		;;
110	esac
111
112	case "$extrafiles" in
113	*/extr/*) fileopts="${fileopts} -E ${tmp}-ext.c1" ;;
114	esac
115	case "$extrafiles" in
116	*/read/*) fileopts="${fileopts} -R ${tmp}-read.c1" ;;
117	esac
118	case "$extrafiles" in
119	*/write/*) fileopts="${fileopts} -W ${tmp}-write.c1" ;;
120	esac
121	case "$extrafiles" in
122	*/sem/*) fileopts="${fileopts} -S ${tmp}-sem.c1" ;;
123	esac
124	case "$extrafiles" in
125	*/semsw/*) fileopts="${fileopts} -X ${tmp}-semsw.c1" ;;
126	esac
127
128	${cgen} ${cgendir}/cgen-sim.scm \
129		-s ${cgendir} \
130		${cgenflags} \
131		-f "${archflags}" \
132		-m ${mach} \
133		-a ${archfile} \
134		-i ${isa} \
135		${fileopts}
136
137	case $action in
138	*cpu*)
139		sed $sedscript < ${tmp}-cpu.h1 > ${tmp}-cpu.h
140		${move_if_change} ${tmp}-cpu.h ${srcdir}/cpu${suffix}.h
141		sed $sedscript < ${tmp}-cpu.c1 > ${tmp}-cpu.c
142		${move_if_change} ${tmp}-cpu.c ${srcdir}/cpu${suffix}.c
143		sed $sedscript < ${tmp}-mod.c1 > ${tmp}-mod.c
144		${move_if_change} ${tmp}-mod.c ${srcdir}/model${suffix}.c
145		rm -f ${tmp}-cpu.h1 ${tmp}-cpu.c1 ${tmp}-mod.c1
146		;;
147	esac
148
149	case $action in
150	*decode*)
151		sed $sedscript < ${tmp}-dec.h1 > ${tmp}-dec.h
152		${move_if_change} ${tmp}-dec.h ${srcdir}/decode${suffix}.h
153		sed $sedscript < ${tmp}-dec.c1 > ${tmp}-dec.c
154		${move_if_change} ${tmp}-dec.c ${srcdir}/decode${suffix}.c
155		rm -f ${tmp}-dec.h1 ${tmp}-dec.c1
156		;;
157	esac
158
159	if test -f ${tmp}-ext.c1 ; then \
160		sed $sedscript < ${tmp}-ext.c1 > ${tmp}-ext.c ; \
161		${move_if_change} ${tmp}-ext.c ${srcdir}/extract${suffix}.c ; \
162		rm -f ${tmp}-ext.c1
163	fi
164	if test -f ${tmp}-read.c1 ; then \
165		sed $sedscript < ${tmp}-read.c1 > ${tmp}-read.c ; \
166		${move_if_change} ${tmp}-read.c ${srcdir}/read${suffix}.c ; \
167		rm -f ${tmp}-read.c1
168	fi
169	if test -f ${tmp}-write.c1 ; then \
170		sed $sedscript < ${tmp}-write.c1 > ${tmp}-write.c ; \
171		${move_if_change} ${tmp}-write.c ${srcdir}/write${suffix}.c ; \
172		rm -f ${tmp}-write.c1
173	fi
174	if test -f ${tmp}-sem.c1 ; then \
175		sed $sedscript < ${tmp}-sem.c1 > ${tmp}-sem.c ; \
176		${move_if_change} ${tmp}-sem.c ${srcdir}/sem${suffix}.c ; \
177		rm -f ${tmp}-sem.c1
178	fi
179	if test -f ${tmp}-semsw.c1 ; then \
180		sed $sedscript < ${tmp}-semsw.c1 > ${tmp}-semsw.c ; \
181		${move_if_change} ${tmp}-semsw.c ${srcdir}/sem${suffix}-switch.c ; \
182		rm -f ${tmp}-semsw.c1
183	fi
184
185	;;
186
187defs)
188	rm -f ${tmp}-defs.h1 ${tmp}-defs.h
189	
190	${cgen} ${cgendir}/cgen-sim.scm \
191		-s ${cgendir} \
192		${cgenflags} \
193		-f "${archflags}" \
194		-m ${mach} \
195		-a ${archfile} \
196		-i ${isa} \
197		-G ${tmp}-defs.h1
198	sed $sedscript < ${tmp}-defs.h1 > ${tmp}-defs.h
199	${move_if_change} ${tmp}-defs.h ${srcdir}/defs${suffix}.h
200	rm -f ${tmp}-defs.h1
201	;;
202
203desc)
204	rm -f ${tmp}-desc.h1 ${tmp}-desc.h
205	rm -f ${tmp}-desc.c1 ${tmp}-desc.c
206	rm -f ${tmp}-opc.h1 ${tmp}-opc.h
207
208	${cgen} ${cgendir}/cgen-opc.scm \
209		-s ${cgendir} \
210		${cgenflags} \
211		-OPC ${opcfile} \
212		-f "${archflags}" \
213		-m ${mach} \
214		-a ${archfile} \
215		-i ${isa} \
216		-H ${tmp}-desc.h1 \
217		-C ${tmp}-desc.c1 \
218		-O ${tmp}-opc.h1
219	sed $sedscript < ${tmp}-desc.h1 > ${tmp}-desc.h
220	${move_if_change} ${tmp}-desc.h ${srcdir}/${arch}-desc.h
221	sed $sedscript < ${tmp}-desc.c1 > ${tmp}-desc.c
222	${move_if_change} ${tmp}-desc.c ${srcdir}/${arch}-desc.c
223	sed $sedscript < ${tmp}-opc.h1 > ${tmp}-opc.h
224	${move_if_change} ${tmp}-opc.h ${srcdir}/${arch}-opc.h
225
226	rm -f ${tmp}-desc.h1 ${tmp}-desc.c1 ${tmp}-opc.h1
227	;;
228
229*)
230	echo "`basename $0`: unknown action: ${action}" >&2
231	exit 1
232	;;
233
234esac
235
236exit 0
237