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());
230 * \@brief Generated register classes from spec.
231 * \@note 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 FIRM_BE_${tmp}_GEN_${tmp}_REGALLOC_IF_T_H
237 #define FIRM_BE_${tmp}_GEN_${tmp}_REGALLOC_IF_T_H
239 #include "gen_${arch}_regalloc_if.h"
243 print OUT @obst_header_t;
245 print OUT "\n#endif\n";
248 # generate header (external usage) file
249 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
251 $creation_time = localtime(time());
256 * \@brief Contains additional external requirements defs for external includes.
257 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
258 * Edit $specfile instead.
259 * created by: $0 $specfile $target_dir
260 * \@date $creation_time
262 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_REGALLOC_IF_H
263 #define FIRM_BE_${tmp}_GEN_${tmp}_REGALLOC_IF_H
265 #include "../bearch.h"
266 #include "${arch}_nodes_attr.h"
270 print OUT @obst_regdef, "\n";
272 print OUT @obst_classdef, "\n";
274 print OUT @obst_regtypes_decl, "\n";
276 print OUT "extern arch_register_class_t $arch\_reg_classes[N_CLASSES];\n\n";
278 print OUT "void ".$arch."_register_init(void *isa_ptr);\n\n";
280 print OUT @obst_header_all, "\n";
282 print OUT "\n#endif\n";
289 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
291 $creation_time = localtime(time());
296 * \@brief 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.
299 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
300 * Edit $specfile instead.
301 * created by: $0 $specfile $target_dir
302 * \$date $creation_time
308 #include "gen_${arch}_regalloc_if.h"
309 #include "gen_${arch}_machine.h"
310 #include "bearch_${arch}_t.h"
311 #include "${arch}_map_regs.h"
317 #define BIT(x) (1 << (x % 32))
321 print OUT "arch_register_class_t ${arch}_reg_classes[] = {\n\t".join(",\n\t", @obst_regclasses)."\n};\n\n";
323 print OUT @obst_regtypes_def, "\n";
325 print OUT "void ${arch}_register_init(void *isa_ptr) {\n";
326 print OUT @obst_reginit;
329 print OUT @obst_limit_func;
336 # Remember the register class for each index in the given requirements.
337 # We need this information for requirements like "in_sX" or "out_dX"
338 # @return array of classes corresponding to the requirement for each index
340 sub build_inout_idx_class {
346 if (exists($n->{"reg_req"}{"$inout"})) {
347 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
349 for (my $idx = 0; $idx <= $#reqs; $idx++) {
352 if ($reqs[$idx] eq "none") {
354 } elsif (is_reg_class($reqs[$idx])) {
355 $class = $reqs[$idx];
357 my @regs = split(/ /, $reqs[$idx]);
358 GET_CLASS: foreach my $reg (@regs) {
359 if ($reg =~ /!?(in|out)\_r\d+/ || $reg =~ /!in/) {
360 $class = "UNKNOWN_CLASS";
363 $class = get_reg_class($reg);
364 if (!defined $class) {
365 die("Could not get ".uc($inout)." register class for '$op' pos $idx (reg $reg) ... exiting.\n");
374 push(@idx_class, $class);
382 # Generates the requirements for the given description
384 sub generate_requirements {
389 # get classes for the complementary direction
390 my $outin = ($inout eq "in") ? "out" : "in";
392 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
394 for (my $idx = 0; $idx <= $#reqs; $idx++) {
397 my $tmp2 = "const arch_register_req_t ${op}_reg_req_${inout}_${idx} = ";
399 if ($reqs[$idx] eq "none") {
401 $tmp2 .= "\tarch_register_req_type_none,\n";
402 $tmp2 .= "\tNULL, /* regclass */\n";
403 $tmp2 .= "\tNULL, /* limit bitset */\n";
404 $tmp2 .= "\t-1, /* same pos */\n";
405 $tmp2 .= "\t-1 /* different pos */\n";
408 push(@obst_req, $tmp2."\n");
409 push(@obst_header_t, "extern const arch_register_req_t ${op}_reg_req_${inout}_${idx};\n");
410 } elsif ($reqs[$idx] =~ /^new_reg_(.*)$/) {
411 if (is_reg_class($1)) {
413 $tmp2 .= "\tarch_register_req_type_should_be_different_from_all,\n";
414 $tmp2 .= "\t&${arch}_reg_classes[CLASS_${arch}_$1],\n";
415 $tmp2 .= "\tNULL, /* limit bitset */\n";
416 $tmp2 .= "\t-1, /* same pos */\n";
417 $tmp2 .= "\t-1 /* different pos */\n";
420 push(@obst_req, $tmp2."\n");
421 push(@obst_header_t, "extern const arch_register_req_t ${op}_reg_req_${inout}_${idx};\n");
423 print STDERR "Invalid register class '$1' given in OUT requirement $idx for '$op'.\n";
425 } elsif (is_reg_class($reqs[$idx])) {
426 my $class = $reqs[$idx];
428 $tmp2 .= "\tarch_register_req_type_normal,\n";
429 $tmp2 .= "\t&${arch}_reg_classes[CLASS_${arch}_${class}],\n";
430 $tmp2 .= "\tNULL, /* limit bitset */\n";
431 $tmp2 .= "\t-1, /* same pos */\n";
432 $tmp2 .= "\t-1 /* different pos */\n";
435 push(@obst_req, $tmp2."\n");
436 push(@obst_header_t, "extern const arch_register_req_t ${op}_reg_req_${inout}_${idx};\n");
439 my ($class, $has_limit, $same_pos, $different_pos) = build_subset_class_func($n, $op, $idx, (($inout eq "in") ? 1 : 0), $reqs[$idx]);
441 if (!defined($class)) {
442 die("Could not build subset for ".uc($inout)." requirements '$op' pos $idx ... exiting.\n");
446 push(@req_type_mask, "arch_register_req_type_limited");
448 if (defined($same_pos)) {
449 push(@req_type_mask, "arch_register_req_type_should_be_same");
451 if (defined($different_pos)) {
452 if ($different_pos == 666) {
453 push(@req_type_mask, "arch_register_req_type_should_be_different_from_all");
454 undef $different_pos;
456 push(@req_type_mask, "arch_register_req_type_should_be_different");
461 $tmp2 .= "\t".join(" | ", @req_type_mask).",\n";
462 $tmp2 .= "\t&${arch}_reg_classes[CLASS_${arch}_${class}],\n";
463 $tmp2 .= "\t".($has_limit ? "limit_reg_${op}_${inout}_${idx}" : "NULL").",\n";
464 $tmp2 .= "\t".(defined($same_pos) ? $same_pos : "-1").",\n";
465 $tmp2 .= "\t".(defined($different_pos) ? $different_pos : "-1")."\n";
468 push(@obst_req, $tmp2."\n");
469 push(@obst_header_t, "extern const arch_register_req_t ${op}_reg_req_${inout}_${idx};\n");
476 # Determines whether $name is a specified register class or not.
477 # @return 1 if name is register class, 0 otherwise
481 return 1 if exists($reg_classes{"$name"});
486 # Returns the register class for a given register.
487 # @return class or undef
491 $reg = substr($reg, 1) if ($reg =~ /!.*/);
492 return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
497 # Returns the index of a given register within it's register class.
498 # @return index or undef
502 return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
507 # Generates the function for a given $op and a given IN-index
508 # which returns a subset of possible register from a register class
509 # @return classname from which the subset is derived or undef and
510 # pos which corresponds to in/out reference position or undef
512 sub build_subset_class_func {
516 my $same_pos = undef;
517 my $different_pos = undef;
524 # build function header
530 my $outin = $in ? "out" : "in";
531 my @regs = split(/ /, shift);
533 my @idx_class = build_inout_idx_class($n, $op, $outin);
535 # set/unset registers
536 CHECK_REQS: foreach (@regs) {
537 if (/(!)?$outin\_r(\d+)/) {
538 if (($1 && defined($different_pos)) || (!$1 && defined($same_pos))) {
539 print STDERR "Multiple in/out references of same type in one requirement not allowed.\n";
540 return (undef, undef, undef, undef);
544 $different_pos = $in ? -$2 : $2 - 1;
547 $same_pos = $in ? -$2 : $2 - 1;
550 $class = $idx_class[$2 - 1];
554 $class = $idx_class[0];
555 return ($class, 0, undef, 666);
559 if (substr($_, 0, 1) eq "!") {
560 if (defined($neg) && $neg == 0) {
561 # we have seen a positiv constraint as first one but this one is negative
562 # this doesn't make sense
563 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
564 return (undef, undef, undef, undef);
567 if (!defined($neg)) {
571 $_ = substr($_, 1); # skip '!'
574 if (defined($neg) && $neg == 1) {
575 # we have seen a negative constraint as first one but this one is positive
576 # this doesn't make sense
577 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
578 return (undef, undef, undef, undef);
585 # check if register belongs to one of the given classes
586 $temp = get_reg_class($_);
587 if (!defined($temp)) {
588 print STDERR "Unknown register '$_'!\n";
589 return (undef, undef, undef, undef);
593 if (!defined($class)) {
595 } elsif ($class ne $temp) {
596 # all registers must belong to the same class
597 print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
598 return (undef, undef, undef, undef);
601 # calculate position inside the initializer bitfield (only 32 bits per
603 my $regidx = get_reg_index($_);
604 my $arrayp = $regidx / 32;
605 push(@{$limit_array[$arrayp]}, $_);
608 # don't allow ignore regs in negative constraints
610 my @cur_class = @{ $reg_classes{"$class"} };
611 for (my $idx = 0; $idx <= $#cur_class; $idx++) {
612 if (defined($cur_class[$idx]{"type"}) && ($cur_class[$idx]{"type"} & 4)) {
613 my $reg = $cur_class[$idx]{"name"};
614 my $regix = get_reg_index($reg);
615 my $arrayp = $regix / 32;
616 push(@{$limit_array[$arrayp]}, $reg);
621 if ($has_limit == 1) {
622 push(@obst_limit_func, "static const unsigned limit_reg_${op}_".($in ? "in" : "out")."_". ${idx} ."[] = { ");
624 my $limitbitsetlen = $regclass2len{$class};
625 my $limitarraylen = $limitbitsetlen / 32 + ($limitbitsetlen % 32 > 0 ? 1 : 0);
626 for(my $i = 0; $i < $limitarraylen; $i++) {
627 my $limitarraypart = $limit_array[$i];
631 push(@obst_limit_func, ", ");
635 $temp = "0xFFFFFFFF";
637 foreach my $reg (@{$limitarraypart}) {
640 } elsif(defined($temp)) {
643 $temp .= "BIT(REG_".uc(${reg}).")";
646 push(@obst_limit_func, "${temp}");
648 push(@obst_limit_func, "0");
651 push(@obst_limit_func, " };\n");
654 return ($class, $has_limit, $same_pos, $different_pos);
658 # Gets the variable name for the execution unit assigned to this register.
660 sub get_execunit_variable_name {
663 my $uc_arch = uc($arch);
667 SRCH: foreach my $cur_type (keys(%cpu)) {
668 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
669 if ($unit eq $cur_unit) {
670 my $tp_name = "$arch\_execution_units_$cur_type";
671 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
672 $name = "&".$tp_name."[".$unit_name."]";
680 print STDERR "Invalid execution unit $unit specified!\n";