1#! /bin/sh
2# CGEN generic assembler support code.
3#
4#   Copyright (C) 2000-2020 Free Software Foundation, Inc.
5#
6#   This file is part of the GNU opcodes library.
7#
8#   This library is free software; you can redistribute it and/or modify
9#   it under the terms of the GNU General Public License as published by
10#   the Free Software Foundation; either version 3, or (at your option)
11#   any later version.
12#
13#   It is distributed in the hope that it will be useful, but WITHOUT
14#   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15#   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
16#   License for more details.
17#
18#   You should have received a copy of the GNU General Public License along
19#   with this program; if not, write to the Free Software Foundation, Inc.,
20#   51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
21#
22# Generate CGEN opcode files: arch-desc.[ch], arch-opc.[ch],
23# arch-asm.c, arch-dis.c, arch-opinst.c, arch-ibld.[ch].
24#
25# Usage:
26# cgen.sh action srcdir cgen cgendir cgenflags arch prefix \
27#         arch-file opc-file options [extrafiles]
28#
29# ACTION is currently always "opcodes". It exists to be consistent with the
30# simulator.
31# ARCH is the name of the architecture.
32# It is substituted into @arch@ and @ARCH@ in the generated files.
33# PREFIX is both the generated file prefix and is substituted into
34# @prefix@ in the generated files.
35# ARCH-FILE is the name of the .cpu file (including path).
36# OPC-FILE is the name of the .opc file (including path).
37# OPTIONS is comma separated list of options (???).
38# EXTRAFILES is a space separated list (1 arg still) of extra files to build:
39#	- opinst - arch-opinst.c is being made, causes semantic analysis
40#
41# We store the generated files in the source directory until we decide to
42# ship a Scheme interpreter (or other implementation) with gdb/binutils.
43# Maybe we never will.
44
45# We want to behave like make, any error forces us to stop.
46set -e
47
48action=$1
49srcdir=$2
50cgen="$3"
51cgendir=$4
52cgenflags=$5
53arch=$6
54prefix=$7
55archfile=$8
56opcfile=$9
57shift ; options=$9
58
59# List of extra files to build.
60# Values: opinst (only 1 extra file at present)
61shift ; extrafiles=$9
62
63rootdir=${srcdir}/..
64move_if_change="${CONFIG_SHELL:-/bin/sh} ${rootdir}/move-if-change"
65
66# $arch is $6, as passed on the command line.
67# $ARCH is the same argument but in all uppercase.
68# Both forms are used in this script.
69
70lowercase='abcdefghijklmnopqrstuvwxyz'
71uppercase='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
72ARCH=`echo ${arch} | tr "${lowercase}" "${uppercase}"`
73
74# Allow parallel makes to run multiple cgen's without colliding.
75tmp=tmp-$$
76
77extrafile_args=""
78for ef in .. $extrafiles
79do
80    case $ef in
81    ..) ;;
82    opinst) extrafile_args="-Q ${tmp}-opinst.c1 $extrafile_args" ;;
83    esac
84done
85
86header="/* DO NOT EDIT!  -*- buffer-read-only: t -*- vi:set ro:  */"
87
88case $action in
89opcodes)
90	# Remove residual working files.
91	rm -f ${tmp}-desc.h ${tmp}-desc.h1
92	rm -f ${tmp}-desc.c ${tmp}-desc.c1
93	rm -f ${tmp}-opc.h ${tmp}-opc.h1
94	rm -f ${tmp}-opc.c ${tmp}-opc.c1
95	rm -f ${tmp}-opinst.c ${tmp}-opinst.c1
96	rm -f ${tmp}-ibld.h ${tmp}-ibld.h1
97	rm -f ${tmp}-ibld.c ${tmp}-ibld.in1
98	rm -f ${tmp}-asm.c ${tmp}-asm.in1
99	rm -f ${tmp}-dis.c ${tmp}-dis.in1
100
101	# Run CGEN.
102	${cgen} ${cgendir}/cgen-opc.scm \
103		-s ${cgendir} \
104		${cgenflags} \
105		-f "${options}" \
106		-m all \
107		-a ${archfile} \
108	        -OPC ${opcfile} \
109		-H ${tmp}-desc.h1 \
110		-C ${tmp}-desc.c1 \
111		-O ${tmp}-opc.h1 \
112		-P ${tmp}-opc.c1 \
113		-L ${tmp}-ibld.in1 \
114		-A ${tmp}-asm.in1 \
115		-D ${tmp}-dis.in1 \
116		${extrafile_args}
117
118	# Customise generated files for the particular architecture.
119	sed -e "1i$header" \
120	    -e "s/@ARCH@/${ARCH}/g" -e "s/@arch@/${arch}/g" \
121	    -e 's/[ 	][ 	]*$//' < ${tmp}-desc.h1 > ${tmp}-desc.h
122	${rootdir}/move-if-change ${tmp}-desc.h ${srcdir}/${prefix}-desc.h
123
124	sed -e "1i$header" \
125	    -e "s/@ARCH@/${ARCH}/g" -e "s/@arch@/${arch}/g" \
126	    -e "s/@prefix@/${prefix}/" -e 's/[ 	][ 	]*$//' \
127	    < ${tmp}-desc.c1 > ${tmp}-desc.c
128	${rootdir}/move-if-change ${tmp}-desc.c ${srcdir}/${prefix}-desc.c
129
130	sed -e "1i$header" \
131	    -e "s/@ARCH@/${ARCH}/g" -e "s/@arch@/${arch}/g" \
132	    -e 's/[ 	][ 	]*$//' < ${tmp}-opc.h1 > ${tmp}-opc.h
133	${rootdir}/move-if-change ${tmp}-opc.h ${srcdir}/${prefix}-opc.h
134
135	sed -e "1i$header" \
136	    -e "s/@ARCH@/${ARCH}/g" -e "s/@arch@/${arch}/g" \
137	    -e "s/@prefix@/${prefix}/" -e 's/[ 	][ 	]*$//' \
138	    < ${tmp}-opc.c1 > ${tmp}-opc.c
139	${rootdir}/move-if-change ${tmp}-opc.c ${srcdir}/${prefix}-opc.c
140
141	case $extrafiles in
142	*opinst*)
143	  sed -e "1i$header" \
144	      -e "s/@ARCH@/${ARCH}/g" -e "s/@arch@/${arch}/g" \
145	      -e "s/@prefix@/${prefix}/" -e 's/[ 	][ 	]*$//' \
146	      < ${tmp}-opinst.c1 >${tmp}-opinst.c
147	  ${rootdir}/move-if-change ${tmp}-opinst.c ${srcdir}/${prefix}-opinst.c
148	  ;;
149	esac
150
151	cat ${srcdir}/cgen-ibld.in ${tmp}-ibld.in1 | \
152	  sed -e "1i$header" \
153	    -e "s/@ARCH@/${ARCH}/g" -e "s/@arch@/${arch}/g" \
154	    -e "s/@prefix@/${prefix}/" -e 's/[ 	][ 	]*$//' > ${tmp}-ibld.c
155	${rootdir}/move-if-change ${tmp}-ibld.c ${srcdir}/${prefix}-ibld.c
156
157	sed -e "/ -- assembler routines/ r ${tmp}-asm.in1" ${srcdir}/cgen-asm.in \
158	  | sed -e "1i$header" \
159		-e "s/@ARCH@/${ARCH}/g" -e "s/@arch@/${arch}/g" \
160		-e "s/@prefix@/${prefix}/" -e 's/[ 	][ 	]*$//' \
161	  > ${tmp}-asm.c
162	${rootdir}/move-if-change ${tmp}-asm.c ${srcdir}/${prefix}-asm.c
163
164	sed -e "/ -- disassembler routines/ r ${tmp}-dis.in1" ${srcdir}/cgen-dis.in \
165	  | sed -e "1i$header" \
166		-e "s/@ARCH@/${ARCH}/g" -e "s/@arch@/${arch}/g" \
167		-e "s/@prefix@/${prefix}/" -e 's/[ 	][ 	]*$//' \
168	  > ${tmp}-dis.c
169	${rootdir}/move-if-change ${tmp}-dis.c ${srcdir}/${prefix}-dis.c
170
171	# Remove temporary files.
172	rm -f ${tmp}-desc.h1 ${tmp}-desc.c1
173	rm -f ${tmp}-opc.h1 ${tmp}-opc.c1
174	rm -f ${tmp}-opinst.c1
175	rm -f ${tmp}-ibld.h1 ${tmp}-ibld.in1
176	rm -f ${tmp}-asm.in1 ${tmp}-dis.in1
177	;;
178
179*)
180	echo "$0: bad action: ${action}" >&2
181	exit 1
182	;;
183
184esac
185
186exit 0
187