#! /usr/pkg/gnu/bin/perl

# acts as the driver for the translation from rcc output to mulsim
# acceptable input 

#	check the command line input, arg should be file.mas

die "usage: $0 filename [-q]\t\tNOTE: ORDER MATTERS!\n\t\t-q\tquiet mode\n" if $#ARGV < 0;

$infile = $ARGV[0];
die if ($infile eq "");
$mcc_path = $ENV{'MCC_PATH'};
if ($mcc_path eq "") {  &PATH_ERR($mcc_path); }

# the sep_stack phase added by NM:

@phases = ("pre_rcc",
	     "lcc",
	     "post_rcc",
	     "sep_stack",
	     "mas_semble",
	     );

for ($idx = 0; $idx <= $#phases; $idx++) {
    push(@perl_scripts, $mcc_path . '/lib/' . $phases[$idx]);
}

foreach $pscr (@perl_scripts) {
    if (! (-x $pscr)) {  &PATH_ERR($mcc_path);  }
}
				# process the silent switch
if ($#ARGV != 1) {
    if ($ARGV[1] eq "-q") {
	$verbose = "";
    }
    else {
	$verbose = "-v";
    }
}
				# generate various file names
$lcc_in = $infile;
substr($lcc_in,-2,2) = "_lccin.c";
$lcc_out = $infile;
substr($lcc_out,-1,1) = "mas";

$executable = $infile;
$executable =~ s/\.c//;

				# do some cleanup
system("rm $lcc_in") if -e $lcc_in;
system("rm $lcc_out") if -e $lcc_out;
system("rm $executable") if -e $executable;

$include_path = $mcc_path . '/include';

# NM also added a command-line argument for sep_stack:

$mas_file = $executable . ".mas";
@comm_args = ("-f $infile -o $lcc_in -p $mcc_path $verbose",
	      "$lcc_in -I$include_path -Wf-target=mulsim-linux -S -o $lcc_out",
	      "-f $lcc_out -p $mcc_path $verbose",
	      " $mas_file",
	      "-f $lcc_out -p $mcc_path -i $include_path -c",
	      );

for ($idx = 0; $idx <= $#perl_scripts; $idx++) {
    push(@commands, "$perl_scripts[$idx] $comm_args[$idx]");
}
				# after this loop the file src.lcc_in will
				# contain the output of the pre_rcc stage
				# 
				# the file src.mas will contain the output
				# of the lcc state
				# 
				# the file exe will be ready simulate
foreach $idx (0 .. $#commands) {
    print STDERR "\n$commands[$idx]\n" if ($verbose);
    $ret_val = system ("$commands[$idx]") / 256;
    &EXIT_ERROR($ret_val, $idx) if $ret_val;
}

print STDERR "\nmcc completed.  Executable is '$executable'.\n";
print STDERR "To call the executable 'diff_name' ";
print STDERR "type 'mv $executable diff_name'  ;)\n"; 

sub PATH_ERR {
    print STDERR "FATAL ERR: Environment variable MCC_PATH ";
    if ($_[0] eq "") {		 
	print STDERR "is not set.\n";
    }
    else {
	print STDERR "is wrong or permissions on files\n";
	print STDERR "\tERR: in lib are incorrect.  ";
	print STDERR "Current value of MCC_PATH is:\n\t\t", $_[0], "\n";
    }
    print STDERR "\tERR: MCC_PATH must be set to ";
    print STDERR "the path containing the mcc compiler.\n";
    print STDERR "\tERR: Do NOT include the final 'bin'.\n";
    print STDERR "\tERR: Good Bye!!\n";
    exit (11);
}

sub EXIT_ERROR {
				# get names for the arguments
    local($err_num, $idx) = @_;
    if ($phases[$idx] eq "lcc") {
	print STDERR "\tLine numbers in error messages for \'",$lcc_in,"\' ";
	print STDERR "match\n\t(approx.) the line numbers in \'", $infile;
	print STDERR "\' be sure to\n\texamine BOTH files.\n";
    }	
    print STDERR "\nERR: mcc died in $phases[$idx] phase (exit value was $err_num).  BYE.\n";
    exit 13;
}
