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
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
29
30rootdir=${srcdir}/../..
31
32if test -z "$isa" ; then
33  isa=all
34  prefix=$cpu
35else
36  prefix=${cpu}_$isa
37fi
38
39lowercase='abcdefghijklmnopqrstuvwxyz'
40uppercase='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
41ARCH=`echo ${arch} | tr "${lowercase}" "${uppercase}"`
42CPU=`echo ${cpu} | tr "${lowercase}" "${uppercase}"`
43PREFIX=`echo ${prefix} | tr "${lowercase}" "${uppercase}"`
44
45sedscript="\
46-e s/@ARCH@/${ARCH}/g -e s/@arch@/${arch}/g \
47-e s/@CPU@/${CPU}/g -e s/@cpu@/${cpu}/g \
48-e s/@PREFIX@/${PREFIX}/g -e s/@prefix@/${prefix}/g"
49
50case $action in
51arch)
52	rm -f tmp-arch.h1 tmp-arch.h
53	rm -f tmp-arch.c1 tmp-arch.c
54	rm -f tmp-all.h1 tmp-all.h
55
56	${cgen} -s ${cgendir}/cgen-sim.scm \
57		-s ${cgendir} \
58		${cgenflags} \
59		-f "${archflags}" \
60		-m ${mach} \
61		-a ${archfile} \
62		-i ${isa} \
63		-A tmp-arch.h1 \
64		-B tmp-arch.c1 \
65		-N tmp-all.h1
66	sed $sedscript < tmp-arch.h1 > tmp-arch.h
67	${rootdir}/move-if-change tmp-arch.h ${srcdir}/arch.h
68	sed $sedscript < tmp-arch.c1 > tmp-arch.c
69	${rootdir}/move-if-change tmp-arch.c ${srcdir}/arch.c
70	sed $sedscript < tmp-all.h1 > tmp-all.h
71	${rootdir}/move-if-change tmp-all.h ${srcdir}/cpuall.h
72
73	rm -f tmp-arch.h1 tmp-arch.c1 tmp-all.h1
74	;;
75
76cpu | decode | cpu-decode)
77
78	fileopts=""
79	case $action in
80	*cpu*)
81		rm -f tmp-cpu.h1 tmp-cpu.c1
82		rm -f tmp-ext.c1 tmp-read.c1 tmp-write.c1
83		rm -f tmp-sem.c1 tmp-semsw.c1
84		rm -f tmp-mod.c1
85		rm -f tmp-cpu.h tmp-cpu.c
86		rm -f tmp-ext.c tmp-read.c tmp-write.c
87		rm -f tmp-sem.c tmp-semsw.c tmp-mod.c
88		fileopts="$fileopts \
89			-C tmp-cpu.h1 \
90			-U tmp-cpu.c1 \
91			-M tmp-mod.c1 \
92			${extrafiles}"
93		;;
94	esac
95	case $action in
96	*decode*)
97		rm -f tmp-dec.h1 tmp-dec.h tmp-dec.c1 tmp-dec.c
98		fileopts="$fileopts \
99			-T tmp-dec.h1 \
100			-D tmp-dec.c1"
101		case "$extrafiles" in
102		  ignored) # Do nothing.
103			   ;;
104		  *)       fileopts="$fileopts $extrafiles"
105			   ;;
106		esac
107		;;
108	esac
109
110	${cgen} -s ${cgendir}/cgen-sim.scm \
111		-s ${cgendir} \
112		${cgenflags} \
113		-f "${archflags}" \
114		-m ${mach} \
115		-a ${archfile} \
116		-i ${isa} \
117		${fileopts}
118
119	case $action in
120	*cpu*)
121		sed $sedscript < tmp-cpu.h1 > tmp-cpu.h
122		${rootdir}/move-if-change tmp-cpu.h ${srcdir}/cpu${suffix}.h
123		sed $sedscript < tmp-cpu.c1 > tmp-cpu.c
124		${rootdir}/move-if-change tmp-cpu.c ${srcdir}/cpu${suffix}.c
125		sed $sedscript < tmp-mod.c1 > tmp-mod.c
126		${rootdir}/move-if-change tmp-mod.c ${srcdir}/model${suffix}.c
127		if test -f tmp-ext.c1 ; then \
128			sed $sedscript < tmp-ext.c1 > tmp-ext.c ; \
129			${rootdir}/move-if-change tmp-ext.c ${srcdir}/extract${suffix}.c ; \
130		fi
131		if test -f tmp-read.c1 ; then \
132			sed $sedscript < tmp-read.c1 > tmp-read.c ; \
133			${rootdir}/move-if-change tmp-read.c ${srcdir}/read${suffix}.c ; \
134		fi
135		if test -f tmp-write.c1 ; then \
136			sed $sedscript < tmp-write.c1 > tmp-write.c ; \
137			${rootdir}/move-if-change tmp-write.c ${srcdir}/write${suffix}.c ; \
138		fi
139		if test -f tmp-sem.c1 ; then \
140			sed $sedscript < tmp-sem.c1 > tmp-sem.c ; \
141			${rootdir}/move-if-change tmp-sem.c ${srcdir}/sem${suffix}.c ; \
142		fi
143		if test -f tmp-semsw.c1 ; then \
144			sed $sedscript < tmp-semsw.c1 > tmp-semsw.c ; \
145			${rootdir}/move-if-change tmp-semsw.c ${srcdir}/sem${suffix}-switch.c ; \
146		fi
147
148		rm -f tmp-cpu.h1 tmp-cpu.c1
149		rm -f tmp-ext.c1 tmp-read.c1 tmp-write.c1
150		rm -f tmp-sem.c1 tmp-semsw.c1 tmp-mod.c1
151		;;
152	esac
153
154	case $action in
155	*decode*)
156		sed $sedscript < tmp-dec.h1 > tmp-dec.h
157		${rootdir}/move-if-change tmp-dec.h ${srcdir}/decode${suffix}.h
158		sed $sedscript < tmp-dec.c1 > tmp-dec.c
159		${rootdir}/move-if-change tmp-dec.c ${srcdir}/decode${suffix}.c
160
161		if test -f tmp-sem.c1 ; then \
162			sed $sedscript < tmp-sem.c1 > tmp-sem.c ; \
163			${rootdir}/move-if-change tmp-sem.c ${srcdir}/sem${suffix}.c ; \
164		fi
165		if test -f tmp-semsw.c1 ; then \
166			sed $sedscript < tmp-semsw.c1 > tmp-semsw.c ; \
167			${rootdir}/move-if-change tmp-semsw.c ${srcdir}/sem${suffix}-switch.c ; \
168		fi
169
170		rm -f tmp-dec.h1 tmp-dec.c1
171		;;
172	esac
173
174	;;
175
176defs)
177	rm -f tmp-defs.h1 tmp-defs.h
178
179	${cgen} -s ${cgendir}/cgen-sim.scm \
180		-s ${cgendir} \
181		${cgenflags} \
182		-f "${archflags}" \
183		-m ${mach} \
184		-a ${archfile} \
185		-i ${isa} \
186		-G tmp-defs.h1
187	sed $sedscript < tmp-defs.h1 > tmp-defs.h
188	${rootdir}/move-if-change tmp-defs.h ${srcdir}/defs${suffix}.h
189	;;
190
191desc)
192	rm -f tmp-desc.h1 tmp-desc.h
193	rm -f tmp-desc.c1 tmp-desc.c
194	rm -f tmp-opc.h1 tmp-opc.h
195
196	${cgen} -s ${cgendir}/cgen-opc.scm \
197		-s ${cgendir} \
198		${cgenflags} \
199		-f "${archflags}" \
200		-m ${mach} \
201		-a ${archfile} \
202		-i ${isa} \
203		-H tmp-desc.h1 \
204		-C tmp-desc.c1 \
205		-O tmp-opc.h1
206	sed $sedscript < tmp-desc.h1 > tmp-desc.h
207	${rootdir}/move-if-change tmp-desc.h ${srcdir}/${arch}-desc.h
208	sed $sedscript < tmp-desc.c1 > tmp-desc.c
209	${rootdir}/move-if-change tmp-desc.c ${srcdir}/${arch}-desc.c
210	sed $sedscript < tmp-opc.h1 > tmp-opc.h
211	${rootdir}/move-if-change tmp-opc.h ${srcdir}/${arch}-opc.h
212
213	rm -f tmp-desc.h1 tmp-desc.c1 tmp-opc.h1
214	;;
215
216*)
217	echo "`basename $0`: unknown action: ${action}" >&2
218	exit 1
219	;;
220
221esac
222
223exit 0
224