start register allocator again, fix typo
[libfirm] / ir / be / scripts / generate_regalloc_if.pl
index 8c94db7..c29dc5d 100755 (executable)
@@ -1,8 +1,7 @@
 #!/usr/bin/perl -w
 
-# This script generates C code which emits assembler code for the
-# assembler ir nodes. It takes a "emit" key from the node specification
-# and substitutes lines starting with . with a corresponding fprintf().
+# This script generates C code which creates ands sets up functions and
+# data structures for the register allocator.
 # Creation: 2005/11/14
 # $Id$
 
@@ -15,6 +14,7 @@ my $target_dir = $ARGV[1];
 our $arch;
 our %reg_classes;
 our %nodes;
+our %cpu;
 
 # include spec file
 
@@ -54,12 +54,21 @@ sub translate_reg_type {
                        push(@types, "arch_register_type_ignore");
                }
 
+               if ($t & 8) {
+                       push(@types, "arch_register_type_joker");
+               }
+
+               if ($t & 16) {
+                       push(@types, "arch_register_type_virtual");
+               }
+
                return join(" | ", @types);
        }
 }
 
 # stacks for output
-my @obst_regtypes;     # stack for the register type variables
+my @obst_regtypes_def; # stack for the register type variables definitions
+my @obst_regtypes_decl;# stack for the register type variables declarations
 my @obst_regclasses;   # stack for the register class variables
 my @obst_classdef;     # stack to define a name for a class index
 my @obst_regdef;       # stack to define a name for a register index
@@ -95,7 +104,7 @@ $tmp .= "};\n\n";
 push(@obst_req, $tmp);
 push(@obst_header_all, "extern const $arch\_register_req_t $arch\_default_req_none;\n");
 
-push(@obst_classdef, "#define N_CLASSES ".scalar(keys(%reg_classes))."\n");
+push(@obst_classdef, "enum reg_classes {\n");
 
 my $class_mode;
 
@@ -109,10 +118,10 @@ foreach my $class_name (keys(%reg_classes)) {
        $class_ptr  = "&".$arch."_reg_classes[CLASS_".$class_name."]";
        $class_mode = pop(@class)->{"mode"};
 
-       push(@obst_regtypes, "#define $numregs ".($#class + 1)."\n");
-       push(@obst_regtypes, "arch_register_t ".$class_name."_regs[$numregs];\n\n");
+       push(@obst_regtypes_decl, "extern arch_register_t ".$class_name."_regs[$numregs];\n");
+       push(@obst_regtypes_def, "arch_register_t ".$class_name."_regs[$numregs];\n");
 
-       push(@obst_classdef, "#define CLASS_$class_name $class_idx\n");
+       push(@obst_classdef, "  CLASS_$class_name = $class_idx,\n");
        push(@obst_regclasses, "{ \"$class_name\", $numregs, NULL, ".$class_name."_regs }");
 
        # there is a default NORMAL requirement for each class
@@ -136,6 +145,7 @@ foreach my $class_name (keys(%reg_classes)) {
        push(@obst_reginit, "  /* Init of all registers in class '$class_name' */\n\n");
        push(@obst_reginit, "  /* set largest possible mode for '$class_name' */\n");
        push(@obst_reginit, "  $arch\_reg_classes[CLASS_".$class_name."].mode = $class_mode;\n\n");
+       push(@obst_regdef, "enum reg_".$class_name."_values {\n");
        foreach (@class) {
                # For each class we build for each of it's member registers a limit function
                # which limits the class to this particular register. We also build the
@@ -172,19 +182,28 @@ foreach my $class_name (keys(%reg_classes)) {
                push(@obst_req, $tmp);
                push(@obst_header_all,"extern const $arch\_register_req_t $arch\_default_req_$class_name\_".$_->{"name"}.";\n");
 
+               # realname is name if not set by user
+               $_->{"realname"} = $_->{"name"} if (! exists($_->{"realname"}));
+
                $reg2class{$_->{"name"}} = { "class" => $old_classname, "index" => $idx }; # remember reg to class for later use
-               push(@obst_regdef, "#define REG_".uc($_->{"name"})." $idx\n");
-               push(@obst_reginit, "  ".$class_name."_regs[$idx].name      = \"".$_->{"name"}."\";\n");
+               push(@obst_regdef, "  REG_".uc($_->{"name"})." = $idx,\n");
+               push(@obst_reginit, "  ".$class_name."_regs[$idx].name      = \"".$_->{"realname"}."\";\n");
                push(@obst_reginit, "  ".$class_name."_regs[$idx].reg_class = $class_ptr;\n");
                push(@obst_reginit, "  ".$class_name."_regs[$idx].index     = $idx;\n");
                push(@obst_reginit, "  ".$class_name."_regs[$idx].type      = ".translate_reg_type($_->{"type"}).";\n");
+               push(@obst_reginit, "  ".$class_name."_regs[$idx].data      = ".get_execunit_variable_name($_->{"unit"}).";\n");
                push(@obst_reginit, "\n");
                $idx++;
        }
+       push(@obst_regdef, "  $numregs = $idx\n");
+       push(@obst_regdef, "};\n\n");
 
        $class_idx++;
 }
 
+push(@obst_classdef, "  N_CLASSES = ".scalar(keys(%reg_classes))."\n");
+push(@obst_classdef, "};\n\n");
+
 # generate node-register constraints
 foreach my $op (keys(%nodes)) {
        my %n = %{ $nodes{"$op"} };
@@ -263,7 +282,7 @@ print OUT @obst_regdef, "\n";
 
 print OUT @obst_classdef, "\n";
 
-print OUT @obst_regtypes, "\n";
+print OUT @obst_regtypes_decl, "\n";
 
 print OUT "extern arch_register_class_t $arch\_reg_classes[N_CLASSES];\n\n";
 
@@ -279,7 +298,7 @@ close(OUT);
 
 
 
-# generate c inline file
+# generate c file
 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
 
 $creation_time = localtime(time());
@@ -296,8 +315,13 @@ print OUT<<EOF;
  * date:       $creation_time
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
 #include "gen_$arch\_regalloc_if.h"
-#include "bearch_$arch\_t.h"   /* we need this to put the caller saved registers into the isa set */
+#include "gen_$arch\_machine.h"  /* we need this, as there can be units assigned to registers */
+#include "bearch_$arch\_t.h"     /* we need this to put the caller saved registers into the isa set */
 #include "$arch\_map_regs.h"
 #include "irmode.h"
 
@@ -305,6 +329,8 @@ EOF
 
 print OUT "arch_register_class_t $arch\_reg_classes[] = {\n  ".join(",\n  ", @obst_regclasses)."\n};\n\n";
 
+print OUT @obst_regtypes_def, "\n";
+
 print OUT "void ".$arch."_register_init(void *isa_ptr) {\n";
 print OUT @obst_reginit;
 print OUT "}\n\n";
@@ -341,7 +367,7 @@ sub build_inout_idx_class {
                        else {
                                my @regs = split(/ /, $reqs[$idx]);
 GET_CLASS:             foreach my $reg (@regs) {
-                                       if ($reg =~ /!?(in|out)\_r\d+/) {
+                                       if ($reg =~ /!?(in|out)\_r\d+/ || $reg =~ /!in/) {
                                                $class = "UNKNOWN_CLASS";
                                        }
                                        else {
@@ -385,6 +411,29 @@ sub generate_requirements {
                if ($reqs[$idx] eq "none") {
                        $tmp .= "&$arch\_default_req_none\n";
                }
+               elsif ($reqs[$idx] =~ /^new_reg_(.*)$/) {
+                       if (is_reg_class($1)) {
+                               $tmp  .=  "&_".$op."_reg_req_$inout\_$idx\n";
+                               $tmp2 .= "{\n";
+                               $tmp2 .= "  {\n";
+                               $tmp2 .= "    arch_register_req_type_should_be_different_from_all,\n";
+                               $tmp2 .= "    &$arch\_reg_classes[CLASS_$arch\_".$1."],\n";
+                               $tmp2 .= "    NULL,        /* limit function */\n";
+                               $tmp2 .= "    NULL,        /* limit environment */\n";
+                               $tmp2 .= "    NULL,        /* same node */\n";
+                               $tmp2 .= "    NULL         /* different node */\n";
+                               $tmp2 .= "  },\n";
+                               $tmp2 .= "  0,\n";
+                               $tmp2 .= "  0\n";
+                               $tmp2 .= "};\n";
+
+                               push(@obst_req, $tmp2."\n");
+                               push(@obst_header_all, "extern const $arch\_register_req_t _".$op."_reg_req_$inout\_$idx;\n");
+                       }
+                       else {
+                               print STDERR "Invalid register class '$1' given in OUT requirement $idx for '$op'.\n";
+                       }
+               }
                elsif (is_reg_class($reqs[$idx])) {
                        $tmp .= "&$arch\_default_req_".$arch."_".$reqs[$idx]."\n";
                }
@@ -403,7 +452,13 @@ sub generate_requirements {
                                push(@req_type_mask, "arch_register_req_type_should_be_same");
                        }
                        if (defined($different_pos)) {
-                               push(@req_type_mask, "arch_register_req_type_should_be_different");
+                               if ($different_pos == 666) {
+                                       push(@req_type_mask, "arch_register_req_type_should_be_different_from_all");
+                                       undef $different_pos;
+                               }
+                               else {
+                                       push(@req_type_mask, "arch_register_req_type_should_be_different");
+                               }
                        }
 
                        $tmp  .= "&_".$op."_reg_req_$inout\_$idx\n";
@@ -473,7 +528,9 @@ sub build_subset_class_func {
        my $same_pos      = undef;
        my $different_pos = undef;
        my $temp;
-       my @temp_obst;
+       my @obst_init;
+       my @obst_limits;
+       my @obst_ignore;
 
 
        # build function header
@@ -505,6 +562,10 @@ CHECK_REQS: foreach (@regs) {
                        $class = $idx_class[$2 - 1];
                        next CHECK_REQS;
                }
+               elsif (/!in/) {
+                       $class = $idx_class[0];
+                       return ($class, 0, undef, 666);
+               }
 
                # check for negate
                if (substr($_, 0, 1) eq "!") {
@@ -517,7 +578,7 @@ CHECK_REQS: foreach (@regs) {
 
                        if (!defined($neg)) {
                                $has_limit = 1;
-                               push(@temp_obst, "  bs = bitset_set_all(bs);     /* allow all register (negative constraints given) */\n");
+                               push(@obst_init, "  bs = bitset_set_all(bs);     /* allow all register (negative constraints given) */\n");
                        }
 
                        $_   = substr($_, 1); # skip '!'
@@ -533,7 +594,7 @@ CHECK_REQS: foreach (@regs) {
 
                        if (!defined($neg)) {
                                $has_limit = 1;
-                               push(@temp_obst, "  bs = bitset_clear_all(bs);   /* disallow all register (positive constraints given) */\n");
+                               push(@obst_init, "  bs = bitset_clear_all(bs);   /* disallow all register (positive constraints given) */\n");
                        }
                        $neg = 0;
                }
@@ -557,11 +618,19 @@ CHECK_REQS: foreach (@regs) {
 
                if ($neg == 1) {
                        $has_limit = 1;
-                       push(@temp_obst, "  bitset_clear(bs, ".get_reg_index($_).");         /* disallow $_ */\n");
+                       push(@obst_limits, "  bitset_clear(bs, ".get_reg_index($_).");         /* disallow $_ */\n");
                }
                else {
                        $has_limit = 1;
-                       push(@temp_obst, "  bitset_set(bs, ".get_reg_index($_).");           /* allow $_ */\n");
+                       push(@obst_limits, "  bitset_set(bs, ".get_reg_index($_).");           /* allow $_ */\n");
+               }
+       }
+
+       my @cur_class = @{ $reg_classes{"$class"} };
+       for (my $idx = 0; $idx <= $#cur_class; $idx++) {
+               if (defined($cur_class[$idx]{"type"}) && ($cur_class[$idx]{"type"} & 4)) {
+                       push(@obst_ignore, "  bitset_clear(bs, ".get_reg_index($cur_class[$idx]{"name"}).");");
+                       push(@obst_ignore, "         /* disallow ignore reg ".$cur_class[$idx]{"name"}." */\n");
                }
        }
 
@@ -570,9 +639,41 @@ CHECK_REQS: foreach (@regs) {
 
                push(@obst_limit_func, "/* limit the possible registers for ".($in ? "IN" : "OUT")." $idx at op $op */\n");
                push(@obst_limit_func, "void limit_reg_".$op."_".($in ? "in" : "out")."_".$idx."(void *_unused, bitset_t *bs) {\n");
-               push(@obst_limit_func, @temp_obst);
+               push(@obst_limit_func, @obst_init);
+               push(@obst_limit_func, @obst_ignore);
+               push(@obst_limit_func, @obst_limits);
                push(@obst_limit_func, "}\n\n");
        }
 
        return ($class, $has_limit, $same_pos, $different_pos);
 }
+
+###
+# Gets the variable name for the execution unit assigned to this register.
+###
+sub get_execunit_variable_name {
+       my $unit    = shift;
+       my $name    = "NULL";
+       my $uc_arch = uc($arch);
+
+       if ($unit) {
+               my $found = 0;
+SRCH:  foreach my $cur_type (keys(%cpu)) {
+                       foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
+                               if ($unit eq $cur_unit) {
+                                       my $tp_name   = "$arch\_execution_units_$cur_type";
+                                       my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
+                                       $name  = "&".$tp_name."[".$unit_name."]";
+                                       $found = 1;
+                                       last SRCH;
+                               }
+                       }
+               }
+
+               if (! $found) {
+                       print STDERR "Invalid execution unit $unit specified!\n";
+               }
+       }
+
+       return $name;
+}