4 # Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
6 # This file is part of libFirm.
8 # This file may be distributed and/or modified under the terms of the
9 # GNU General Public License version 2 as published by the Free Software
10 # Foundation and appearing in the file LICENSE.GPL included in the
11 # packaging of this file.
13 # Licensees holding valid libFirm Professional Edition licenses may use
14 # this file in accordance with the libFirm Commercial License.
15 # Agreement provided with the Software.
17 # This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 # WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 # This script generates C code which creates ands sets up functions and
23 # data structures for the register allocator.
24 # Creation: 2005/11/14
31 my $specfile = $ARGV[0];
32 my $target_dir = $ARGV[1];
45 unless ($return = do $specfile) {
46 die "couldn't parse $specfile: $@" if $@;
47 die "couldn't do $specfile: $!" unless defined $return;
48 die "couldn't run $specfile" unless $return;
52 my $target_c = $target_dir."/gen_".$arch."_regalloc_if.c";
53 my $target_h = $target_dir."/gen_".$arch."_regalloc_if.h";
54 my $target_h_t = $target_dir."/gen_".$arch."_regalloc_if_t.h";
57 sub translate_reg_type {
61 return "arch_register_type_none";
67 push(@types, "arch_register_type_caller_save");
71 push(@types, "arch_register_type_callee_save");
75 push(@types, "arch_register_type_ignore");
79 push(@types, "arch_register_type_joker");
83 push(@types, "arch_register_type_virtual");
87 push(@types, "arch_register_type_state");
90 return join(" | ", @types);
95 my @obst_regtypes_def; # stack for the register type variables definitions
96 my @obst_regtypes_decl;# stack for the register type variables declarations
97 my @obst_regclasses; # stack for the register class variables
98 my @obst_classdef; # stack to define a name for a class index
99 my @obst_regdef; # stack to define a name for a register index
100 my @obst_reginit; # stack for the register type inits
101 my @obst_req; # stack for the register requirements
102 my @obst_limit_func; # stack for functions to return a subset of a register class
103 my @obst_header_all; # stack for some extern struct defs needed for bearch_$arch include
115 push(@obst_classdef, "enum reg_classes {\n");
119 # assure, the initialization is done only once
120 push(@obst_reginit, "\tstatic int run_once = 0;\n");
121 push(@obst_reginit, "\n");
122 push(@obst_reginit, "\tif (run_once)\n");
123 push(@obst_reginit, "\t\treturn;\n");
124 push(@obst_reginit, "\trun_once = 1;\n");
126 # generate register type and class variable, init function and default requirements
127 foreach my $class_name (keys(%reg_classes)) {
128 my @class = @{ $reg_classes{"$class_name"} };
129 my $old_classname = $class_name;
131 $class_name = $arch."_".$class_name;
132 $numregs = "N_".$class_name."_REGS";
133 $class_ptr = "&".$arch."_reg_classes[CLASS_".$class_name."]";
134 $class_mode = pop(@class)->{"mode"};
136 push(@obst_regtypes_decl, "extern const arch_register_t ${class_name}_regs[$numregs];\n");
138 push(@obst_classdef, "\tCLASS_$class_name = $class_idx,\n");
139 push(@obst_regclasses, "{ \"$class_name\", $numregs, NULL, ".$class_name."_regs }");
142 push(@obst_reginit, "\t/* set largest possible mode for '$class_name' */\n");
143 push(@obst_reginit, "\t$arch\_reg_classes[CLASS_".$class_name."].mode = $class_mode;\n\n");
144 push(@obst_regtypes_def, "const arch_register_t ${class_name}_regs[$numregs] = {\n");
146 push(@obst_regdef, "enum reg_${class_name}_indices {\n");
148 my $ucname = uc($_->{"name"});
149 my $type = translate_reg_type($_->{"type"});
150 # realname is name if not set by user
151 $_->{"realname"} = $_->{"name"} if (! exists($_->{"realname"}));
152 my $realname = $_->{realname};
153 my $execunitvarname = get_execunit_variable_name($_->{"unit"});
156 $reg2class{$_->{"name"}} = { "class" => $old_classname, "index" => $idx }; # remember reg to class for later use
157 push(@obst_regdef, "\tREG_${ucname},\n");
159 push(@obst_regtypes_def, "\t{\n");
160 push(@obst_regtypes_def, "\t\t\"$realname\",\n");
161 push(@obst_regtypes_def, "\t\t$class_ptr,\n");
162 push(@obst_regtypes_def, "\t\tREG_${ucname},\n");
163 push(@obst_regtypes_def, "\t\t$type,\n");
164 push(@obst_regtypes_def, "\t\t$execunitvarname\n");
165 push(@obst_regtypes_def, "\t},\n");
167 # push(@obst_reginit, "\t${class_name}_regs[$idx].name = \"".$_->{"realname"}."\";\n");
168 # push(@obst_reginit, "\t${class_name}_regs[$idx].reg_class = $class_ptr;\n");
169 # push(@obst_reginit, "\t${class_name}_regs[$idx].index = $idx;\n");
170 # push(@obst_reginit, "\t${class_name}_regs[$idx].type = ".translate_reg_type($_->{"type"}).";\n");
171 # push(@obst_reginit, "\t${class_name}_regs[$idx].data = ".get_execunit_variable_name($_->{"unit"}).";\n");
172 # push(@obst_reginit, "\n");
175 push(@obst_regtypes_def, "};\n");
177 $regclass2len{$old_classname} = $idx;
178 push(@obst_regdef, "\t$numregs = $idx\n");
179 push(@obst_regdef, "};\n\n");
184 push(@obst_regdef, "enum flag_indices {\n");
185 foreach my $flag (keys(%flags)) {
186 my %f = %{ $flags{$flag} };
188 push(@obst_regdef, "\tFLAG_$flag,\n");
190 push(@obst_regdef, "\tFLAG_LAST\n");
191 push(@obst_regdef, "};\n");
192 push(@obst_regtypes_decl, "extern arch_flag_t ${arch}_flags[];\n");
194 push(@obst_classdef, "\tN_CLASSES = ".scalar(keys(%reg_classes))."\n");
195 push(@obst_classdef, "};\n\n");
197 # generate node-register constraints
198 foreach my $op (keys(%nodes)) {
199 my %n = %{ $nodes{$op} };
201 next if (!exists($n{"reg_req"}));
205 push(@obst_req, "/* IN requirements for '$op' */\n");
206 # check for argument requirements
207 if (exists($n{"reg_req"}{"in"})) {
208 generate_requirements(\%n, $op, "in");
211 push(@obst_req, "/* OUT requirements for '$op' */\n");
212 # check for result requirements
213 if (exists($n{"reg_req"}{"out"})) {
214 generate_requirements(\%n, $op, "out");
220 # generate header _t (internal usage) file
221 open(OUT, ">$target_h_t") || die("Could not open $target_h_t, reason: $!\n");
223 my $creation_time = localtime(time());
229 * Generated register classes from spec.
231 * DO NOT EDIT THIS FILE, your changes will be lost.
232 * Edit $specfile instead.
233 * created by: $0 $specfile $target_dir
234 * date: $creation_time
236 #ifndef _GEN_${tmp}_REGALLOC_IF_T_H_
237 #define _GEN_${tmp}_REGALLOC_IF_T_H_
239 #include "gen_${arch}_regalloc_if.h"
243 print OUT @obst_header_t;
245 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_T_H_ */\n";
249 # generate header (external usage) file
250 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
252 $creation_time = localtime(time());
256 * Contains additional external requirements defs for external includes.
258 * DO NOT EDIT THIS FILE, your changes will be lost.
259 * Edit $specfile instead.
260 * created by: $0 $specfile $target_dir
261 * date: $creation_time
263 #ifndef _GEN_${tmp}_REGALLOC_IF_H_
264 #define _GEN_${tmp}_REGALLOC_IF_H_
266 #include "../bearch.h"
267 #include "${arch}_nodes_attr.h"
271 print OUT @obst_regdef, "\n";
273 print OUT @obst_classdef, "\n";
275 print OUT @obst_regtypes_decl, "\n";
277 print OUT "extern arch_register_class_t $arch\_reg_classes[N_CLASSES];\n\n";
279 print OUT "void ".$arch."_register_init(void *isa_ptr);\n\n";
281 print OUT @obst_header_all, "\n";
283 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_H_ */\n";
290 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
292 $creation_time = localtime(time());
296 * The generated interface for the register allocator.
297 * Contains register classes and types and register constraints
298 * for all nodes where constraints were given in spec.
300 * DO NOT EDIT THIS FILE, your changes will be lost.
301 * Edit $specfile instead.
302 * created by: $0 $specfile $target_dir
303 * date: $creation_time
309 #include "gen_${arch}_regalloc_if.h"
310 #include "gen_${arch}_machine.h"
311 #include "bearch_${arch}_t.h"
312 #include "${arch}_map_regs.h"
318 #define BIT(x) (1 << (x % 32))
322 print OUT "arch_register_class_t ${arch}_reg_classes[] = {\n\t".join(",\n\t", @obst_regclasses)."\n};\n\n";
324 print OUT @obst_regtypes_def, "\n";
326 print OUT "void ${arch}_register_init(void *isa_ptr) {\n";
327 print OUT @obst_reginit;
330 print OUT @obst_limit_func;
337 # Remember the register class for each index in the given requirements.
338 # We need this information for requirements like "in_sX" or "out_dX"
339 # @return array of classes corresponding to the requirement for each index
341 sub build_inout_idx_class {
347 if (exists($n->{"reg_req"}{"$inout"})) {
348 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
350 for (my $idx = 0; $idx <= $#reqs; $idx++) {
353 if ($reqs[$idx] eq "none") {
355 } elsif (is_reg_class($reqs[$idx])) {
356 $class = $reqs[$idx];
358 my @regs = split(/ /, $reqs[$idx]);
359 GET_CLASS: foreach my $reg (@regs) {
360 if ($reg =~ /!?(in|out)\_r\d+/ || $reg =~ /!in/) {
361 $class = "UNKNOWN_CLASS";
364 $class = get_reg_class($reg);
365 if (!defined $class) {
366 die("Could not get ".uc($inout)." register class for '$op' pos $idx (reg $reg) ... exiting.\n");
375 push(@idx_class, $class);
383 # Generates the requirements for the given description
385 sub generate_requirements {
390 # get classes for the complementary direction
391 my $outin = ($inout eq "in") ? "out" : "in";
393 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
395 for (my $idx = 0; $idx <= $#reqs; $idx++) {
398 my $tmp2 = "const arch_register_req_t ${op}_reg_req_${inout}_${idx} = ";
400 if ($reqs[$idx] eq "none") {
402 $tmp2 .= "\tarch_register_req_type_none,\n";
403 $tmp2 .= "\tNULL, /* regclass */\n";
404 $tmp2 .= "\tNULL, /* limit bitset */\n";
405 $tmp2 .= "\t-1, /* same pos */\n";
406 $tmp2 .= "\t-1 /* different pos */\n";
409 push(@obst_req, $tmp2."\n");
410 push(@obst_header_t, "extern const arch_register_req_t ${op}_reg_req_${inout}_${idx};\n");
411 } elsif ($reqs[$idx] =~ /^new_reg_(.*)$/) {
412 if (is_reg_class($1)) {
414 $tmp2 .= "\tarch_register_req_type_should_be_different_from_all,\n";
415 $tmp2 .= "\t&${arch}_reg_classes[CLASS_${arch}_$1],\n";
416 $tmp2 .= "\tNULL, /* limit bitset */\n";
417 $tmp2 .= "\t-1, /* same pos */\n";
418 $tmp2 .= "\t-1 /* different pos */\n";
421 push(@obst_req, $tmp2."\n");
422 push(@obst_header_t, "extern const arch_register_req_t ${op}_reg_req_${inout}_${idx};\n");
424 print STDERR "Invalid register class '$1' given in OUT requirement $idx for '$op'.\n";
426 } elsif (is_reg_class($reqs[$idx])) {
427 my $class = $reqs[$idx];
429 $tmp2 .= "\tarch_register_req_type_normal,\n";
430 $tmp2 .= "\t&${arch}_reg_classes[CLASS_${arch}_${class}],\n";
431 $tmp2 .= "\tNULL, /* limit bitset */\n";
432 $tmp2 .= "\t-1, /* same pos */\n";
433 $tmp2 .= "\t-1 /* different pos */\n";
436 push(@obst_req, $tmp2."\n");
437 push(@obst_header_t, "extern const arch_register_req_t ${op}_reg_req_${inout}_${idx};\n");
440 my ($class, $has_limit, $same_pos, $different_pos) = build_subset_class_func($n, $op, $idx, (($inout eq "in") ? 1 : 0), $reqs[$idx]);
442 if (!defined($class)) {
443 die("Could not build subset for ".uc($inout)." requirements '$op' pos $idx ... exiting.\n");
447 push(@req_type_mask, "arch_register_req_type_limited");
449 if (defined($same_pos)) {
450 push(@req_type_mask, "arch_register_req_type_should_be_same");
452 if (defined($different_pos)) {
453 if ($different_pos == 666) {
454 push(@req_type_mask, "arch_register_req_type_should_be_different_from_all");
455 undef $different_pos;
457 push(@req_type_mask, "arch_register_req_type_should_be_different");
462 $tmp2 .= "\t".join(" | ", @req_type_mask).",\n";
463 $tmp2 .= "\t&${arch}_reg_classes[CLASS_${arch}_${class}],\n";
464 $tmp2 .= "\t".($has_limit ? "limit_reg_${op}_${inout}_${idx}" : "NULL").",\n";
465 $tmp2 .= "\t".(defined($same_pos) ? $same_pos : "-1").",\n";
466 $tmp2 .= "\t".(defined($different_pos) ? $different_pos : "-1")."\n";
469 push(@obst_req, $tmp2."\n");
470 push(@obst_header_t, "extern const arch_register_req_t ${op}_reg_req_${inout}_${idx};\n");
477 # Determines whether $name is a specified register class or not.
478 # @return 1 if name is register class, 0 otherwise
482 return 1 if exists($reg_classes{"$name"});
487 # Returns the register class for a given register.
488 # @return class or undef
492 $reg = substr($reg, 1) if ($reg =~ /!.*/);
493 return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
498 # Returns the index of a given register within it's register class.
499 # @return index or undef
503 return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
508 # Generates the function for a given $op and a given IN-index
509 # which returns a subset of possible register from a register class
510 # @return classname from which the subset is derived or undef and
511 # pos which corresponds to in/out reference position or undef
513 sub build_subset_class_func {
517 my $same_pos = undef;
518 my $different_pos = undef;
525 # build function header
531 my $outin = $in ? "out" : "in";
532 my @regs = split(/ /, shift);
534 my @idx_class = build_inout_idx_class($n, $op, $outin);
536 # set/unset registers
537 CHECK_REQS: foreach (@regs) {
538 if (/(!)?$outin\_r(\d+)/) {
539 if (($1 && defined($different_pos)) || (!$1 && defined($same_pos))) {
540 print STDERR "Multiple in/out references of same type in one requirement not allowed.\n";
541 return (undef, undef, undef, undef);
545 $different_pos = $in ? -$2 : $2 - 1;
548 $same_pos = $in ? -$2 : $2 - 1;
551 $class = $idx_class[$2 - 1];
555 $class = $idx_class[0];
556 return ($class, 0, undef, 666);
560 if (substr($_, 0, 1) eq "!") {
561 if (defined($neg) && $neg == 0) {
562 # we have seen a positiv constraint as first one but this one is negative
563 # this doesn't make sense
564 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
565 return (undef, undef, undef, undef);
568 if (!defined($neg)) {
572 $_ = substr($_, 1); # skip '!'
575 if (defined($neg) && $neg == 1) {
576 # we have seen a negative constraint as first one but this one is positive
577 # this doesn't make sense
578 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
579 return (undef, undef, undef, undef);
586 # check if register belongs to one of the given classes
587 $temp = get_reg_class($_);
588 if (!defined($temp)) {
589 print STDERR "Unknown register '$_'!\n";
590 return (undef, undef, undef, undef);
594 if (!defined($class)) {
596 } elsif ($class ne $temp) {
597 # all registers must belong to the same class
598 print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
599 return (undef, undef, undef, undef);
602 # calculate position inside the initializer bitfield (only 32 bits per
604 my $regidx = get_reg_index($_);
605 my $arrayp = $regidx / 32;
606 push(@{$limit_array[$arrayp]}, $_);
609 # don't allow ignore regs in negative constraints
611 my @cur_class = @{ $reg_classes{"$class"} };
612 for (my $idx = 0; $idx <= $#cur_class; $idx++) {
613 if (defined($cur_class[$idx]{"type"}) && ($cur_class[$idx]{"type"} & 4)) {
614 my $reg = $cur_class[$idx]{"name"};
615 my $regix = get_reg_index($reg);
616 my $arrayp = $regix / 32;
617 push(@{$limit_array[$arrayp]}, $reg);
622 if ($has_limit == 1) {
623 push(@obst_limit_func, "static const unsigned limit_reg_${op}_".($in ? "in" : "out")."_". ${idx} ."[] = { ");
625 my $limitbitsetlen = $regclass2len{$class};
626 my $limitarraylen = $limitbitsetlen / 32 + ($limitbitsetlen % 32 > 0 ? 1 : 0);
627 for(my $i = 0; $i < $limitarraylen; $i++) {
628 my $limitarraypart = $limit_array[$i];
632 push(@obst_limit_func, ", ");
636 $temp = "0xFFFFFFFF";
638 foreach my $reg (@{$limitarraypart}) {
641 } elsif(defined($temp)) {
644 $temp .= "BIT(REG_".uc(${reg}).")";
647 push(@obst_limit_func, "${temp}");
649 push(@obst_limit_func, "0");
652 push(@obst_limit_func, " };\n");
655 return ($class, $has_limit, $same_pos, $different_pos);
659 # Gets the variable name for the execution unit assigned to this register.
661 sub get_execunit_variable_name {
664 my $uc_arch = uc($arch);
668 SRCH: foreach my $cur_type (keys(%cpu)) {
669 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
670 if ($unit eq $cur_unit) {
671 my $tp_name = "$arch\_execution_units_$cur_type";
672 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
673 $name = "&".$tp_name."[".$unit_name."]";
681 print STDERR "Invalid execution unit $unit specified!\n";