#!/bin/sh

echo "compiling assembly for simpvir.c ..."
./compile-simpvir.sh

echo "compiling schant-int.c ..."
./compile-schant-with-shmall.sh int vm
mv schant-int bin

echo "compiling schant-bigfloat8.c ..."
./compile-schant-with-shmall.sh bigfloat8 object
mv schant-bigfloat8 bin

echo "compiling schant-bigfloat12.c ..."
./compile-schant-with-shmall.sh bigfloat12 object
mv schant-bigfloat12 bin

cat bin/shmall.txt src/simpvir/simpvir.c | bin/schant-int >xxx.scm

echo "recreating simpvir-x86-32-core.asm ..."
echo "x86 32" >compilation-mode.txt
cat bin/schimper.txt compilation-mode.txt xxx.scm | bin/schant-int >xxx
diff xxx src/asm/simpvir-x86-32-core.asm 
if [ $? = 0 ]; then
  echo "compilation of simpvir.c to x86 32 target is identical to original"
else
  echo "compilation of simpvir.c to x86 32 target is not identical to original"
fi

echo "recreating simpvir-x86-64-core.asm ..."
echo "x86 64" >compilation-mode.txt
cat bin/schimper.txt compilation-mode.txt xxx.scm | bin/schant-int >xxx
diff xxx src/asm/simpvir-x86-64-core.asm 
if [ $? = 0 ]; then
  echo "compilation of simpvir.c to x86 64 target is identical to original"
else
  echo "compilation of simpvir.c to x86 64 target is not identical to original"
fi

POOLGAP=350
echo "recreating simpvir-arm6-core.s ..."
echo "arm v6 $POOLGAP" >compilation-mode.txt
cat bin/schimper.txt compilation-mode.txt xxx.scm | bin/schant-int >xxx
diff xxx src/asm/simpvir-arm6-core.s
if [ $? = 0 ]; then
  echo "compilation of simpvir.c to arm6 target is identical to original"
else
  echo "compilation of simpvir.c to arm6 target is not identical to original"
fi

echo "recreating simpvir-arm7-core.s ..."
echo "arm v7 $POOLGAP" >compilation-mode.txt
cat bin/schimper.txt compilation-mode.txt xxx.scm | bin/schant-int >xxx
diff xxx src/asm/simpvir-arm7-core.s
if [ $? = 0 ]; then
  echo "compilation of simpvir.c to arm7 target is identical to original"
else
  echo "compilation of simpvir.c to arm7 target is not identical to original"
fi

echo "recreating simpvir-aarch64-core.s ..."
echo "arm aarch64 $POOLGAP" >compilation-mode.txt
cat bin/schimper.txt compilation-mode.txt xxx.scm | bin/schant-int >xxx
diff xxx src/asm/simpvir-aarch64-core.s
if [ $? = 0 ]; then
  echo "compilation of simpvir.c to aarch64 target is identical to original"
else
  echo "compilation of simpvir.c to aarch64 target is not identical to original"
fi

echo "recreating simpvir-riscv-core.s ..."
echo riscv >compilation-mode.txt
cat bin/schimper.txt compilation-mode.txt xxx.scm | bin/schant-int >xxx
diff xxx src/asm/simpvir-riscv-core.s
if [ $? = 0 ]; then
  echo "compilation of simpvir.c to riscv target is identical to original"
else
  echo "compilation of simpvir.c to riscv target is not identical to original"
fi

rm xxx.scm

echo "recreating shmall C compiler ..."
./compile-shmall-with-shmall.sh
diff bin/shmall.txt shmall.txt
if [ $? = 0 ]; then
  echo "compilation of the shmall C compiler using the shmall C compiler produces output identical to original"
else
  echo "compilation of the shmall C compiler using the shmall C compiler does not produce output identical to original"
fi
rm shmall.txt

echo "recreating schant-compiler ..."
cat bin/schant-compiler.txt src/scheme/schant-compiler.scm | \
  bin/schant-bigfloat12 >xxx
diff bin/schant-compiler.txt xxx
if [ $? = 0 ]; then
  echo "compilation of the schant-compiler using the schant-compiler produces output identical to original"
else
  echo "compilation of the schant-compiler using the schant-compiler does not produce output identical to original"
fi
rm xxx

echo "recreating simpvir-assembler ..."
cat bin/schant-compiler.txt src/scheme/simpvir-assembler.scm | \
  bin/schant-int >xxx
diff bin/simpvir-assembler.txt xxx
if [ $? = 0 ]; then
  echo "compilation of the simpvir-assembler using the schant-compiler produces output identical to original"
else
  echo "compilation of the simpvir-assembler using the schant-compiler does not produce output identical to original"
fi
rm xxx

echo "recreating schimper ..."
cat bin/schant-compiler.txt src/scheme/schimper.scm | \
  bin/schant-int >xxx
diff bin/schimper.txt xxx
if [ $? = 0 ]; then
  echo "compilation of schimper using the schant-compiler produces output identical to original"
else
  echo "compilation of schimper using the schant-compiler does not produce output identical to original"
fi
rm xxx

echo "recreating schant-int simpvir bytecodes ..."
./compile-schant-int-to-simpvir-bytecodes.sh
diff bin/schant-int.vm.txt schant-int.vm.txt
if [ $? = 0 ]; then
  echo "compilation of schant-int to simpvir bytecodes output identical to original"
else
  echo "compilation of schant-int to simpvir bytecodes does not produce output identical to original"
fi
rm schant-int.vm.txt

echo "running simpvir tests ..."

cd tests

./test-simpvir.sh
echo "running scheme tests ..."
./test-scheme.sh
echo "running C tests ..."
./test-c.sh

cd ..

echo "build the peephole optimizer"
SIZE=30000
MACHINE=`uname -m`
if [ $MACHINE = riscv64 ]; then
  SIZE=100000
fi
./compile-with-shmall-to-native.sh src/peephole/peep object $SIZE
mv src/peephole/peep bin

echo "recompile schant with the peephole optimizer"

echo "recompiling schant-int.c ..."
./compile-schant-with-shmall.sh int object
mv schant-int bin

echo "recompiling schant-bigfloat8.c ..."
./compile-schant-with-shmall.sh bigfloat8 object
mv schant-bigfloat8 bin

echo "recompiling schant-bigfloat12.c ..."
./compile-schant-with-shmall.sh bigfloat12 object
mv schant-bigfloat12 bin
