#!/usr/bin/perl

if (@ARGV != 2)
{
	print "Use: CPL3toAS.pl inputfile outputfile\n";
	print "     outputfile should not include the .s extension\n";
	exit (1);
}

print "Running CPL3toCPP Converter\n";
my $result = qx /java -cp BlackBox CPL3toCPP $ARGV[0] $ARGV[1].cpp/;
print $result;

if ($result ne "There were errors!\n")
{
	print "Adding necessary headers\n";
	system ("cat BlackBox/SPIMIO.h BlackBox/types.h $ARGV[1].cpp > $ARGV[1].cpp.temp");

	print "Cleaning up temporary files\n";
	system ("mv $ARGV[1].cpp.temp $ARGV[1].cpp");

	print "Converting from C++ to Assembly\n";
	system ("mipsel-linux-g++ -S -mips1 $ARGV[1].cpp");

	print "Fixing Assembly for SPIM\n";
	system ("perl BlackBox/spimStripper.pl $ARGV[1].s");
}
