sub Preprocess()

in maxas/MaxAs/MaxAs.pm [748:811]


sub Preprocess
{
    my ($file, $include, $debug, $regMap) = @_;

    my $constMap = {};
    my $removeRegMap;
    if ($regMap)
        { $removeRegMap = 1; }
    else
        { $regMap = {}; }

    # include nested files
    1 while $file =~ s|$IncludeRe| IncludeFile($1, $include) |eg;

    # Strip out comments
    $file =~ s|$CommentRe||g;

    # Execute the CODE sections (old way to run code, to be deprecated)
    1 while $file =~ s|$CodeRe|
        my $out = eval "package MaxAs::MaxAs::CODE; $2";
        $@ ? die("CODE:\n$2\n\nError: $@\n") : $out |eg;

    # Execute the inline code (new way)
    $file =~ s|$InlineRe|
        my ($type, $code) = ($1, $2);
        my $out = eval "package MaxAs::MaxAs::CODE; $code";
        $@ ? die("CODE:\n$code\n\nError: $@\n") : $type eq "+" ? $out : "" |eg;

    #Pull in the constMap
    $file =~ s/$ConstMapRe/ setConstMap($constMap, $1) /eg;

    my @newFile;
    foreach my $line (split "\n", $file)
    {
        # skip comments
        if ($line !~ m'^\s*(?:#|//).*')
        {
            $line =~ s|(\w+(?:\[\d+\])?)| exists $constMap->{$1} ? $constMap->{$1} : $1 |eg;
        }
        push @newFile, $line;
    }
    $file = join "\n", @newFile;

    # Pull in the reg map first as the Scheduler will need it to handle vector instructions
    # Remove the regmap if we're going on to assemble
    $file =~ s/$RegMapRe/ setRegisterMap($regMap, $1); $removeRegMap ? '' : $& /eg;

    # Pick out the SCHEDULE_BLOCK sections
    my @schedBlocks = $file =~ /$ScheduleRe/g;

    # Schedule them
    foreach my $i (0 .. $#schedBlocks)
    {
        # XMAD macros should only appear in SCHEDULE_BLOCKs
        $schedBlocks[$i] = replaceXMADs($schedBlocks[$i]);

        $schedBlocks[$i] = Scheduler($schedBlocks[$i], $i+1, $regMap, $debug);
    }

    # Replace the results
    $file =~ s|$ScheduleRe| shift @schedBlocks |eg;

    return $file;
}