3 # This script generates C code which creates ands sets up functions and
4 # data structures for the register allocator.
12 my $specfile = $ARGV[0];
13 my $target_dir = $ARGV[1];
26 unless ($return = do $specfile) {
27 die "couldn't parse $specfile: $@" if $@;
28 die "couldn't do $specfile: $!" unless defined $return;
29 die "couldn't run $specfile" unless $return;
33 my $target_c = $target_dir."/gen_".$arch."_regalloc_if.c";
34 my $target_h = $target_dir."/gen_".$arch."_regalloc_if.h";
35 my $target_h_t = $target_dir."/gen_".$arch."_regalloc_if_t.h";
38 sub translate_reg_type {
42 return "arch_register_type_none";
48 push(@types, "arch_register_type_caller_save");
52 push(@types, "arch_register_type_callee_save");
56 push(@types, "arch_register_type_ignore");
60 push(@types, "arch_register_type_joker");
64 push(@types, "arch_register_type_virtual");
68 push(@types, "arch_register_type_state");
71 return join(" | ", @types);
76 my @obst_regtypes_def; # stack for the register type variables definitions
77 my @obst_regtypes_decl;# stack for the register type variables declarations
78 my @obst_regclasses; # stack for the register class variables
79 my @obst_classdef; # stack to define a name for a class index
80 my @obst_regdef; # stack to define a name for a register index
81 my @obst_reginit; # stack for the register type inits
82 my @obst_req; # stack for the register requirements
83 my @obst_limit_func; # stack for functions to return a subset of a register class
84 my @obst_header_all; # stack for some extern struct defs needed for bearch_$arch include
96 push(@obst_classdef, "enum reg_classes {\n");
100 # assure, the initialization is done only once
101 push(@obst_reginit, "\tstatic int run_once = 0;\n");
102 push(@obst_reginit, "\n");
103 push(@obst_reginit, "\tif (run_once)\n");
104 push(@obst_reginit, "\t\treturn;\n");
105 push(@obst_reginit, "\trun_once = 1;\n");
107 # generate register type and class variable, init function and default requirements
108 foreach my $class_name (keys(%reg_classes)) {
109 my @class = @{ $reg_classes{"$class_name"} };
110 my $old_classname = $class_name;
112 $class_name = $arch."_".$class_name;
113 $numregs = "N_".$class_name."_REGS";
114 $class_ptr = "&".$arch."_reg_classes[CLASS_".$class_name."]";
115 $class_mode = pop(@class)->{"mode"};
117 push(@obst_regtypes_decl, "extern const arch_register_t ${class_name}_regs[$numregs];\n");
119 push(@obst_classdef, "\tCLASS_$class_name = $class_idx,\n");
120 push(@obst_regclasses, "{ \"$class_name\", $numregs, NULL, ".$class_name."_regs }");
123 push(@obst_reginit, "\t/* set largest possible mode for '$class_name' */\n");
124 push(@obst_reginit, "\t$arch\_reg_classes[CLASS_".$class_name."].mode = $class_mode;\n\n");
125 push(@obst_regtypes_def, "const arch_register_t ${class_name}_regs[$numregs] = {\n");
127 push(@obst_regdef, "enum reg_${class_name}_indices {\n");
129 my $ucname = uc($_->{"name"});
130 my $type = translate_reg_type($_->{"type"});
131 # realname is name if not set by user
132 $_->{"realname"} = $_->{"name"} if (! exists($_->{"realname"}));
133 my $realname = $_->{realname};
134 my $execunitvarname = get_execunit_variable_name($_->{"unit"});
137 $reg2class{$_->{"name"}} = { "class" => $old_classname, "index" => $idx }; # remember reg to class for later use
138 push(@obst_regdef, "\tREG_${ucname},\n");
140 push(@obst_regtypes_def, "\t{\n");
141 push(@obst_regtypes_def, "\t\t\"$realname\",\n");
142 push(@obst_regtypes_def, "\t\t$class_ptr,\n");
143 push(@obst_regtypes_def, "\t\tREG_${ucname},\n");
144 push(@obst_regtypes_def, "\t\t$type,\n");
145 push(@obst_regtypes_def, "\t\t$execunitvarname\n");
146 push(@obst_regtypes_def, "\t},\n");
148 # push(@obst_reginit, "\t${class_name}_regs[$idx].name = \"".$_->{"realname"}."\";\n");
149 # push(@obst_reginit, "\t${class_name}_regs[$idx].reg_class = $class_ptr;\n");
150 # push(@obst_reginit, "\t${class_name}_regs[$idx].index = $idx;\n");
151 # push(@obst_reginit, "\t${class_name}_regs[$idx].type = ".translate_reg_type($_->{"type"}).";\n");
152 # push(@obst_reginit, "\t${class_name}_regs[$idx].data = ".get_execunit_variable_name($_->{"unit"}).";\n");
153 # push(@obst_reginit, "\n");
156 push(@obst_regtypes_def, "};\n");
158 $regclass2len{$old_classname} = $idx;
159 push(@obst_regdef, "\t$numregs = $idx\n");
160 push(@obst_regdef, "};\n\n");
165 push(@obst_regdef, "enum flag_indices {\n");
166 foreach my $flag (keys(%flags)) {
167 my %f = %{ $flags{$flag} };
169 push(@obst_regdef, "\tFLAG_$flag,\n");
171 push(@obst_regdef, "\tFLAG_LAST\n");
172 push(@obst_regdef, "};\n");
173 push(@obst_regtypes_decl, "extern arch_flag_t ${arch}_flags[];\n");
175 push(@obst_classdef, "\tN_CLASSES = ".scalar(keys(%reg_classes))."\n");
176 push(@obst_classdef, "};\n\n");
178 # generate node-register constraints
179 foreach my $op (keys(%nodes)) {
180 my %n = %{ $nodes{$op} };
182 next if (!exists($n{"reg_req"}));
186 push(@obst_req, "/* IN requirements for '$op' */\n");
187 # check for argument requirements
188 if (exists($n{"reg_req"}{"in"})) {
189 generate_requirements(\%n, $op, "in");
192 push(@obst_req, "/* OUT requirements for '$op' */\n");
193 # check for result requirements
194 if (exists($n{"reg_req"}{"out"})) {
195 generate_requirements(\%n, $op, "out");
201 # generate header _t (internal usage) file
202 open(OUT, ">$target_h_t") || die("Could not open $target_h_t, reason: $!\n");
204 my $creation_time = localtime(time());
210 * Generated register classes from spec.
212 * DO NOT EDIT THIS FILE, your changes will be lost.
213 * Edit $specfile instead.
214 * created by: $0 $specfile $target_dir
215 * date: $creation_time
217 #ifndef _GEN_${tmp}_REGALLOC_IF_T_H_
218 #define _GEN_${tmp}_REGALLOC_IF_T_H_
220 #include "gen_${arch}_regalloc_if.h"
224 print OUT @obst_header_t;
226 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_T_H_ */\n";
230 # generate header (external usage) file
231 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
233 $creation_time = localtime(time());
237 * Contains additional external requirements defs for external includes.
239 * DO NOT EDIT THIS FILE, your changes will be lost.
240 * Edit $specfile instead.
241 * created by: $0 $specfile $target_dir
242 * date: $creation_time
244 #ifndef _GEN_${tmp}_REGALLOC_IF_H_
245 #define _GEN_${tmp}_REGALLOC_IF_H_
247 #include "../bearch.h"
248 #include "${arch}_nodes_attr.h"
252 print OUT @obst_regdef, "\n";
254 print OUT @obst_classdef, "\n";
256 print OUT @obst_regtypes_decl, "\n";
258 print OUT "extern arch_register_class_t $arch\_reg_classes[N_CLASSES];\n\n";
260 print OUT "void ".$arch."_register_init(void *isa_ptr);\n\n";
262 print OUT @obst_header_all, "\n";
264 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_H_ */\n";
271 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
273 $creation_time = localtime(time());
277 * The generated interface for the register allocator.
278 * Contains register classes and types and register constraints
279 * for all nodes where constraints were given in spec.
281 * DO NOT EDIT THIS FILE, your changes will be lost.
282 * Edit $specfile instead.
283 * created by: $0 $specfile $target_dir
284 * date: $creation_time
290 #include "gen_${arch}_regalloc_if.h"
291 #include "gen_${arch}_machine.h"
292 #include "bearch_${arch}_t.h"
293 #include "${arch}_map_regs.h"
299 #define BIT(x) (1 << (x % 32))
303 print OUT "arch_register_class_t ${arch}_reg_classes[] = {\n\t".join(",\n\t", @obst_regclasses)."\n};\n\n";
305 print OUT @obst_regtypes_def, "\n";
307 print OUT "void ${arch}_register_init(void *isa_ptr) {\n";
308 print OUT @obst_reginit;
311 print OUT @obst_limit_func;
318 # Remember the register class for each index in the given requirements.
319 # We need this information for requirements like "in_sX" or "out_dX"
320 # @return array of classes corresponding to the requirement for each index
322 sub build_inout_idx_class {
328 if (exists($n->{"reg_req"}{"$inout"})) {
329 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
331 for (my $idx = 0; $idx <= $#reqs; $idx++) {
334 if ($reqs[$idx] eq "none") {
336 } elsif (is_reg_class($reqs[$idx])) {
337 $class = $reqs[$idx];
339 my @regs = split(/ /, $reqs[$idx]);
340 GET_CLASS: foreach my $reg (@regs) {
341 if ($reg =~ /!?(in|out)\_r\d+/ || $reg =~ /!in/) {
342 $class = "UNKNOWN_CLASS";
345 $class = get_reg_class($reg);
346 if (!defined $class) {
347 die("Could not get ".uc($inout)." register class for '$op' pos $idx (reg $reg) ... exiting.\n");
356 push(@idx_class, $class);
364 # Generates the requirements for the given description
366 sub generate_requirements {
371 # get classes for the complementary direction
372 my $outin = ($inout eq "in") ? "out" : "in";
374 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
376 for (my $idx = 0; $idx <= $#reqs; $idx++) {
379 my $tmp2 = "const arch_register_req_t ${op}_reg_req_${inout}_${idx} = ";
381 if ($reqs[$idx] eq "none") {
383 $tmp2 .= "\tarch_register_req_type_none,\n";
384 $tmp2 .= "\tNULL, /* regclass */\n";
385 $tmp2 .= "\tNULL, /* limit bitset */\n";
386 $tmp2 .= "\t-1, /* same pos */\n";
387 $tmp2 .= "\t-1 /* different pos */\n";
390 push(@obst_req, $tmp2."\n");
391 push(@obst_header_t, "extern const arch_register_req_t ${op}_reg_req_${inout}_${idx};\n");
392 } elsif ($reqs[$idx] =~ /^new_reg_(.*)$/) {
393 if (is_reg_class($1)) {
395 $tmp2 .= "\tarch_register_req_type_should_be_different_from_all,\n";
396 $tmp2 .= "\t&${arch}_reg_classes[CLASS_${arch}_$1],\n";
397 $tmp2 .= "\tNULL, /* limit bitset */\n";
398 $tmp2 .= "\t-1, /* same pos */\n";
399 $tmp2 .= "\t-1 /* different pos */\n";
402 push(@obst_req, $tmp2."\n");
403 push(@obst_header_t, "extern const arch_register_req_t ${op}_reg_req_${inout}_${idx};\n");
405 print STDERR "Invalid register class '$1' given in OUT requirement $idx for '$op'.\n";
407 } elsif (is_reg_class($reqs[$idx])) {
408 my $class = $reqs[$idx];
410 $tmp2 .= "\tarch_register_req_type_normal,\n";
411 $tmp2 .= "\t&${arch}_reg_classes[CLASS_${arch}_${class}],\n";
412 $tmp2 .= "\tNULL, /* limit bitset */\n";
413 $tmp2 .= "\t-1, /* same pos */\n";
414 $tmp2 .= "\t-1 /* different pos */\n";
417 push(@obst_req, $tmp2."\n");
418 push(@obst_header_t, "extern const arch_register_req_t ${op}_reg_req_${inout}_${idx};\n");
421 my ($class, $has_limit, $same_pos, $different_pos) = build_subset_class_func($n, $op, $idx, (($inout eq "in") ? 1 : 0), $reqs[$idx]);
423 if (!defined($class)) {
424 die("Could not build subset for ".uc($inout)." requirements '$op' pos $idx ... exiting.\n");
428 push(@req_type_mask, "arch_register_req_type_limited");
430 if (defined($same_pos)) {
431 push(@req_type_mask, "arch_register_req_type_should_be_same");
433 if (defined($different_pos)) {
434 if ($different_pos == 666) {
435 push(@req_type_mask, "arch_register_req_type_should_be_different_from_all");
436 undef $different_pos;
438 push(@req_type_mask, "arch_register_req_type_should_be_different");
443 $tmp2 .= "\t".join(" | ", @req_type_mask).",\n";
444 $tmp2 .= "\t&${arch}_reg_classes[CLASS_${arch}_${class}],\n";
445 $tmp2 .= "\t".($has_limit ? "limit_reg_${op}_${inout}_${idx}" : "NULL").",\n";
446 $tmp2 .= "\t".(defined($same_pos) ? $same_pos : "-1").",\n";
447 $tmp2 .= "\t".(defined($different_pos) ? $different_pos : "-1")."\n";
450 push(@obst_req, $tmp2."\n");
451 push(@obst_header_t, "extern const arch_register_req_t ${op}_reg_req_${inout}_${idx};\n");
458 # Determines whether $name is a specified register class or not.
459 # @return 1 if name is register class, 0 otherwise
463 return 1 if exists($reg_classes{"$name"});
468 # Returns the register class for a given register.
469 # @return class or undef
473 $reg = substr($reg, 1) if ($reg =~ /!.*/);
474 return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
479 # Returns the index of a given register within it's register class.
480 # @return index or undef
484 return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
489 # Generates the function for a given $op and a given IN-index
490 # which returns a subset of possible register from a register class
491 # @return classname from which the subset is derived or undef and
492 # pos which corresponds to in/out reference position or undef
494 sub build_subset_class_func {
498 my $same_pos = undef;
499 my $different_pos = undef;
506 # build function header
512 my $outin = $in ? "out" : "in";
513 my @regs = split(/ /, shift);
515 my @idx_class = build_inout_idx_class($n, $op, $outin);
517 # set/unset registers
518 CHECK_REQS: foreach (@regs) {
519 if (/(!)?$outin\_r(\d+)/) {
520 if (($1 && defined($different_pos)) || (!$1 && defined($same_pos))) {
521 print STDERR "Multiple in/out references of same type in one requirement not allowed.\n";
522 return (undef, undef, undef, undef);
526 $different_pos = $in ? -$2 : $2 - 1;
529 $same_pos = $in ? -$2 : $2 - 1;
532 $class = $idx_class[$2 - 1];
536 $class = $idx_class[0];
537 return ($class, 0, undef, 666);
541 if (substr($_, 0, 1) eq "!") {
542 if (defined($neg) && $neg == 0) {
543 # we have seen a positiv constraint as first one but this one is negative
544 # this doesn't make sense
545 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
546 return (undef, undef, undef, undef);
549 if (!defined($neg)) {
553 $_ = substr($_, 1); # skip '!'
556 if (defined($neg) && $neg == 1) {
557 # we have seen a negative constraint as first one but this one is positive
558 # this doesn't make sense
559 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
560 return (undef, undef, undef, undef);
567 # check if register belongs to one of the given classes
568 $temp = get_reg_class($_);
569 if (!defined($temp)) {
570 print STDERR "Unknown register '$_'!\n";
571 return (undef, undef, undef, undef);
575 if (!defined($class)) {
577 } elsif ($class ne $temp) {
578 # all registers must belong to the same class
579 print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
580 return (undef, undef, undef, undef);
583 # calculate position inside the initializer bitfield (only 32 bits per
585 my $regidx = get_reg_index($_);
586 my $arrayp = $regidx / 32;
587 push(@{$limit_array[$arrayp]}, $_);
590 # don't allow ignore regs in negative constraints
592 my @cur_class = @{ $reg_classes{"$class"} };
593 for (my $idx = 0; $idx <= $#cur_class; $idx++) {
594 if (defined($cur_class[$idx]{"type"}) && ($cur_class[$idx]{"type"} & 4)) {
595 my $reg = $cur_class[$idx]{"name"};
596 my $regix = get_reg_index($reg);
597 my $arrayp = $regix / 32;
598 push(@{$limit_array[$arrayp]}, $reg);
603 if ($has_limit == 1) {
604 push(@obst_limit_func, "static const unsigned limit_reg_${op}_".($in ? "in" : "out")."_". ${idx} ."[] = { ");
606 my $limitbitsetlen = $regclass2len{$class};
607 my $limitarraylen = $limitbitsetlen / 32 + ($limitbitsetlen % 32 > 0 ? 1 : 0);
608 for(my $i = 0; $i < $limitarraylen; $i++) {
609 my $limitarraypart = $limit_array[$i];
613 push(@obst_limit_func, ", ");
617 $temp = "0xFFFFFFFF";
619 foreach my $reg (@{$limitarraypart}) {
622 } elsif(defined($temp)) {
625 $temp .= "BIT(REG_".uc(${reg}).")";
628 push(@obst_limit_func, "${temp}");
630 push(@obst_limit_func, "0");
633 push(@obst_limit_func, " };\n");
636 return ($class, $has_limit, $same_pos, $different_pos);
640 # Gets the variable name for the execution unit assigned to this register.
642 sub get_execunit_variable_name {
645 my $uc_arch = uc($arch);
649 SRCH: foreach my $cur_type (keys(%cpu)) {
650 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
651 if ($unit eq $cur_unit) {
652 my $tp_name = "$arch\_execution_units_$cur_type";
653 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
654 $name = "&".$tp_name."[".$unit_name."]";
662 print STDERR "Invalid execution unit $unit specified!\n";