#! /bin/sh

# Time-stamp: <2006-10-19 09:56:26 feeley>

# Utility to change the number of registers in the virtual machine.
#
# Usage:  misc/set-regs [ <NB_GVM_REGS> [ <NB_ARG_REGS> ]]

NB_GVM_REGS=$1
NB_ARG_REGS=$2

if [ "$NB_GVM_REGS" = "" ] ; then
  NB_GVM_REGS=5
fi

if [ "$NB_ARG_REGS" = "" ] ; then
  NB_ARG_REGS=3
fi

if [ "$NB_GVM_REGS" -lt 3 ] ; then
  echo "NB_GVM_REGS=$NB_ARG_REGS but it must be >= 3"
  exit 1
fi

if [ "$NB_GVM_REGS" -gt 25 ] ; then
  echo "NB_GVM_REGS=$NB_ARG_REGS but it must be <= 25"
  exit 1
fi

if [ "$NB_ARG_REGS" -lt 1 ] ; then
  echo "NB_ARG_REGS=$NB_ARG_REGS but it must be >= 1"
  exit 1
fi

if [ "$NB_ARG_REGS" -gt 12 ] ; then
  echo "NB_ARG_REGS=$NB_ARG_REGS but it must be <= 12"
  exit 1
fi

if [ "$NB_ARG_REGS" -gt `expr $NB_GVM_REGS - 2` ] ; then
  echo "NB_ARG_REGS=$NB_ARG_REGS but it must be <= `expr $NB_GVM_REGS - 2`"
  exit 1
fi

# Save the current compiler just in case something goes wrong.

cp gsc/gsc gsc/gsc-old

# Set the number of registers in the gsc/_t-c-1.scm file.

sed -e "s/(set! targ-nb-gvm-regs [0-9]*)/(set! targ-nb-gvm-regs $NB_GVM_REGS)/" -e "s/(set! targ-nb-arg-regs [0-9]*)/(set! targ-nb-arg-regs $NB_ARG_REGS)/" gsc/_t-c-1.scm > gsc/_t-c-1.scm-new

mv gsc/_t-c-1.scm-new gsc/_t-c-1.scm

# Build the new compiler in gsc/gsc and save it just in case.

make -j 2

cp gsc/gsc gsc/gsc-new

# Set the number of registers in the include/gambit.h file.

sed -e "s/#define ___NB_GVM_REGS [0-9]*/#define ___NB_GVM_REGS $NB_GVM_REGS/" -e "s/#define ___NB_ARG_REGS [0-9]*/#define ___NB_ARG_REGS $NB_ARG_REGS/" include/gambit.h > include/gambit.h-new

mv include/gambit.h-new include/gambit.h

# All the .c files previously generated by gsc are now invalid.

make clean

# Build the new runtime library and interpreter using the new compiler.

cd lib
make -j 2
cd ../gsi
make -j 2
cd ..
