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 # generate register type and class variable, init function and default requirements
120 foreach my $class_name (keys(%reg_classes)) {
121 my @class = @{ $reg_classes{"$class_name"} };
122 my $old_classname = $class_name;
124 $class_name = $arch."_".$class_name;
125 $numregs = "N_".$class_name."_REGS";
126 $class_ptr = "&".$arch."_reg_classes[CLASS_".$class_name."]";
127 $class_mode = pop(@class)->{"mode"};
129 push(@obst_regtypes_decl, "extern const arch_register_t ${class_name}_regs[$numregs];\n");
131 push(@obst_classdef, "\tCLASS_$class_name = $class_idx,\n");
132 push(@obst_regclasses, "{ \"$class_name\", $numregs, NULL, ".$class_name."_regs }");
135 push(@obst_reginit, "\t/* set largest possible mode for '$class_name' */\n");
136 push(@obst_reginit, "\t$arch\_reg_classes[CLASS_".$class_name."].mode = $class_mode;\n\n");
137 push(@obst_regtypes_def, "const arch_register_t ${class_name}_regs[$numregs] = {\n");
139 push(@obst_regdef, "enum reg_${class_name}_indices {\n");
141 my $ucname = uc($_->{"name"});
142 my $type = translate_reg_type($_->{"type"});
143 # realname is name if not set by user
144 $_->{"realname"} = $_->{"name"} if (! exists($_->{"realname"}));
145 my $realname = $_->{realname};
146 my $execunitvarname = get_execunit_variable_name($_->{"unit"});
149 $reg2class{$_->{"name"}} = { "class" => $old_classname, "index" => $idx }; # remember reg to class for later use
150 push(@obst_regdef, "\tREG_${ucname},\n");
152 push(@obst_regtypes_def, "\t{\n");
153 push(@obst_regtypes_def, "\t\t\"$realname\",\n");
154 push(@obst_regtypes_def, "\t\t$class_ptr,\n");
155 push(@obst_regtypes_def, "\t\tREG_${ucname},\n");
156 push(@obst_regtypes_def, "\t\t$type,\n");
157 push(@obst_regtypes_def, "\t\t$execunitvarname\n");
158 push(@obst_regtypes_def, "\t},\n");
160 # push(@obst_reginit, "\t${class_name}_regs[$idx].name = \"".$_->{"realname"}."\";\n");
161 # push(@obst_reginit, "\t${class_name}_regs[$idx].reg_class = $class_ptr;\n");
162 # push(@obst_reginit, "\t${class_name}_regs[$idx].index = $idx;\n");
163 # push(@obst_reginit, "\t${class_name}_regs[$idx].type = ".translate_reg_type($_->{"type"}).";\n");
164 # push(@obst_reginit, "\t${class_name}_regs[$idx].data = ".get_execunit_variable_name($_->{"unit"}).";\n");
165 # push(@obst_reginit, "\n");
168 push(@obst_regtypes_def, "};\n");
170 $regclass2len{$old_classname} = $idx;
171 push(@obst_regdef, "\t$numregs = $idx\n");
172 push(@obst_regdef, "};\n\n");
177 push(@obst_regdef, "enum flag_indices {\n");
178 foreach my $flag (keys(%flags)) {
179 my %f = %{ $flags{$flag} };
181 push(@obst_regdef, "\tFLAG_$flag,\n");
183 push(@obst_regdef, "\tFLAG_LAST\n");
184 push(@obst_regdef, "};\n");
185 push(@obst_regtypes_decl, "extern arch_flag_t ${arch}_flags[];\n");
187 push(@obst_classdef, "\tN_CLASSES = ".scalar(keys(%reg_classes))."\n");
188 push(@obst_classdef, "};\n\n");
190 # generate node-register constraints
191 foreach my $op (keys(%nodes)) {
192 my %n = %{ $nodes{$op} };
194 next if (!exists($n{"reg_req"}));
198 push(@obst_req, "/* IN requirements for '$op' */\n");
199 # check for argument requirements
200 if (exists($n{"reg_req"}{"in"})) {
201 generate_requirements(\%n, $op, "in");
204 push(@obst_req, "/* OUT requirements for '$op' */\n");
205 # check for result requirements
206 if (exists($n{"reg_req"}{"out"})) {
207 generate_requirements(\%n, $op, "out");
213 # generate header _t (internal usage) file
214 open(OUT, ">$target_h_t") || die("Could not open $target_h_t, reason: $!\n");
216 my $creation_time = localtime(time());
223 * \@brief Generated register classes from spec.
224 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
225 * Edit $specfile instead.
226 * created by: $0 $specfile $target_dir
227 * \$date $creation_time
229 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_REGALLOC_IF_T_H
230 #define FIRM_BE_${tmp}_GEN_${tmp}_REGALLOC_IF_T_H
232 #include "gen_${arch}_regalloc_if.h"
236 print OUT @obst_header_t;
238 print OUT "\n#endif\n";
241 # generate header (external usage) file
242 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
244 $creation_time = localtime(time());
249 * \@brief Contains additional external requirements defs for external includes.
250 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
251 * Edit $specfile instead.
252 * created by: $0 $specfile $target_dir
253 * \@date $creation_time
255 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_REGALLOC_IF_H
256 #define FIRM_BE_${tmp}_GEN_${tmp}_REGALLOC_IF_H
258 #include "../bearch.h"
259 #include "${arch}_nodes_attr.h"
263 print OUT @obst_regdef, "\n";
265 print OUT @obst_classdef, "\n";
267 print OUT @obst_regtypes_decl, "\n";
269 print OUT "extern arch_register_class_t $arch\_reg_classes[N_CLASSES];\n\n";
271 print OUT "void ".$arch."_register_init(void *isa_ptr);\n\n";
273 print OUT @obst_header_all, "\n";
275 print OUT "\n#endif\n";
282 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
284 $creation_time = localtime(time());
289 * \@brief The generated interface for the register allocator.
290 * Contains register classes and types and register constraints
291 * for all nodes where constraints were given in spec.
292 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
293 * Edit $specfile instead.
294 * created by: $0 $specfile $target_dir
295 * \$date $creation_time
301 #include "gen_${arch}_regalloc_if.h"
302 #include "gen_${arch}_machine.h"
303 #include "bearch_${arch}_t.h"
304 #include "${arch}_map_regs.h"
310 #define BIT(x) (1 << (x % 32))
314 print OUT "arch_register_class_t ${arch}_reg_classes[] = {\n\t".join(",\n\t", @obst_regclasses)."\n};\n\n";
316 print OUT @obst_regtypes_def, "\n";
318 print OUT "void ${arch}_register_init(void *isa_ptr) {\n";
319 print OUT @obst_reginit;
322 print OUT @obst_limit_func;
329 # Remember the register class for each index in the given requirements.
330 # We need this information for requirements like "in_sX" or "out_dX"
331 # @return array of classes corresponding to the requirement for each index
333 sub build_inout_idx_class {
339 if (exists($n->{"reg_req"}{"$inout"})) {
340 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
342 for (my $idx = 0; $idx <= $#reqs; $idx++) {
345 if ($reqs[$idx] eq "none") {
347 } elsif (is_reg_class($reqs[$idx])) {
348 $class = $reqs[$idx];
350 my @regs = split(/ /, $reqs[$idx]);
351 GET_CLASS: foreach my $reg (@regs) {
352 if ($reg =~ /!?(in|out)\_r\d+/ || $reg =~ /!in/) {
353 $class = "UNKNOWN_CLASS";
356 $class = get_reg_class($reg);
357 if (!defined $class) {
358 die("Could not get ".uc($inout)." register class for '$op' pos $idx (reg $reg) ... exiting.\n");
367 push(@idx_class, $class);
375 # Generates the requirements for the given description
377 sub generate_requirements {
382 # get classes for the complementary direction
383 my $outin = ($inout eq "in") ? "out" : "in";
385 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
387 for (my $idx = 0; $idx <= $#reqs; $idx++) {
390 my $tmp2 = "const arch_register_req_t ${op}_reg_req_${inout}_${idx} = ";
392 if ($reqs[$idx] eq "none") {
394 $tmp2 .= "\tarch_register_req_type_none,\n";
395 $tmp2 .= "\tNULL, /* regclass */\n";
396 $tmp2 .= "\tNULL, /* limit bitset */\n";
397 $tmp2 .= "\t-1, /* same pos */\n";
398 $tmp2 .= "\t-1 /* different pos */\n";
401 push(@obst_req, $tmp2."\n");
402 push(@obst_header_t, "extern const arch_register_req_t ${op}_reg_req_${inout}_${idx};\n");
403 } elsif ($reqs[$idx] =~ /^new_reg_(.*)$/) {
404 if (is_reg_class($1)) {
406 $tmp2 .= "\tarch_register_req_type_should_be_different_from_all,\n";
407 $tmp2 .= "\t&${arch}_reg_classes[CLASS_${arch}_$1],\n";
408 $tmp2 .= "\tNULL, /* limit bitset */\n";
409 $tmp2 .= "\t-1, /* same pos */\n";
410 $tmp2 .= "\t-1 /* different pos */\n";
413 push(@obst_req, $tmp2."\n");
414 push(@obst_header_t, "extern const arch_register_req_t ${op}_reg_req_${inout}_${idx};\n");
416 print STDERR "Invalid register class '$1' given in OUT requirement $idx for '$op'.\n";
418 } elsif (is_reg_class($reqs[$idx])) {
419 my $class = $reqs[$idx];
421 $tmp2 .= "\tarch_register_req_type_normal,\n";
422 $tmp2 .= "\t&${arch}_reg_classes[CLASS_${arch}_${class}],\n";
423 $tmp2 .= "\tNULL, /* limit bitset */\n";
424 $tmp2 .= "\t-1, /* same pos */\n";
425 $tmp2 .= "\t-1 /* different pos */\n";
428 push(@obst_req, $tmp2."\n");
429 push(@obst_header_t, "extern const arch_register_req_t ${op}_reg_req_${inout}_${idx};\n");
432 my ($class, $has_limit, $same_pos, $different_pos) = build_subset_class_func($n, $op, $idx, (($inout eq "in") ? 1 : 0), $reqs[$idx]);
434 if (!defined($class)) {
435 die("Could not build subset for ".uc($inout)." requirements '$op' pos $idx ... exiting.\n");
439 push(@req_type_mask, "arch_register_req_type_limited");
441 if (defined($same_pos)) {
442 push(@req_type_mask, "arch_register_req_type_should_be_same");
444 if (defined($different_pos)) {
445 if ($different_pos == 666) {
446 push(@req_type_mask, "arch_register_req_type_should_be_different_from_all");
447 undef $different_pos;
449 push(@req_type_mask, "arch_register_req_type_should_be_different");
454 $tmp2 .= "\t".join(" | ", @req_type_mask).",\n";
455 $tmp2 .= "\t&${arch}_reg_classes[CLASS_${arch}_${class}],\n";
456 $tmp2 .= "\t".($has_limit ? "limit_reg_${op}_${inout}_${idx}" : "NULL").",\n";
457 $tmp2 .= "\t".(defined($same_pos) ? $same_pos : "-1").",\n";
458 $tmp2 .= "\t".(defined($different_pos) ? $different_pos : "-1")."\n";
461 push(@obst_req, $tmp2."\n");
462 push(@obst_header_t, "extern const arch_register_req_t ${op}_reg_req_${inout}_${idx};\n");
469 # Determines whether $name is a specified register class or not.
470 # @return 1 if name is register class, 0 otherwise
474 return 1 if exists($reg_classes{"$name"});
479 # Returns the register class for a given register.
480 # @return class or undef
484 $reg = substr($reg, 1) if ($reg =~ /!.*/);
485 return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
490 # Returns the index of a given register within it's register class.
491 # @return index or undef
495 return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
500 # Generates the function for a given $op and a given IN-index
501 # which returns a subset of possible register from a register class
502 # @return classname from which the subset is derived or undef and
503 # pos which corresponds to in/out reference position or undef
505 sub build_subset_class_func {
509 my $same_pos = undef;
510 my $different_pos = undef;
517 # build function header
523 my $outin = $in ? "out" : "in";
524 my @regs = split(/ /, shift);
526 my @idx_class = build_inout_idx_class($n, $op, $outin);
528 # set/unset registers
529 CHECK_REQS: foreach (@regs) {
530 if (/(!)?$outin\_r(\d+)/) {
531 if (($1 && defined($different_pos)) || (!$1 && defined($same_pos))) {
532 print STDERR "Multiple in/out references of same type in one requirement not allowed.\n";
533 return (undef, undef, undef, undef);
537 $different_pos = $in ? -$2 : $2 - 1;
540 $same_pos = $in ? -$2 : $2 - 1;
543 $class = $idx_class[$2 - 1];
547 $class = $idx_class[0];
548 return ($class, 0, undef, 666);
552 if (substr($_, 0, 1) eq "!") {
553 if (defined($neg) && $neg == 0) {
554 # we have seen a positiv constraint as first one but this one is negative
555 # this doesn't make sense
556 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
557 return (undef, undef, undef, undef);
560 if (!defined($neg)) {
564 $_ = substr($_, 1); # skip '!'
567 if (defined($neg) && $neg == 1) {
568 # we have seen a negative constraint as first one but this one is positive
569 # this doesn't make sense
570 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
571 return (undef, undef, undef, undef);
578 # check if register belongs to one of the given classes
579 $temp = get_reg_class($_);
580 if (!defined($temp)) {
581 print STDERR "Unknown register '$_'!\n";
582 return (undef, undef, undef, undef);
586 if (!defined($class)) {
588 } elsif ($class ne $temp) {
589 # all registers must belong to the same class
590 print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
591 return (undef, undef, undef, undef);
594 # calculate position inside the initializer bitfield (only 32 bits per
596 my $regidx = get_reg_index($_);
597 my $arrayp = $regidx / 32;
598 push(@{$limit_array[$arrayp]}, $_);
601 # don't allow ignore regs in negative constraints
603 my @cur_class = @{ $reg_classes{"$class"} };
604 for (my $idx = 0; $idx <= $#cur_class; $idx++) {
605 if (defined($cur_class[$idx]{"type"}) && ($cur_class[$idx]{"type"} & 4)) {
606 my $reg = $cur_class[$idx]{"name"};
607 my $regix = get_reg_index($reg);
608 my $arrayp = $regix / 32;
609 push(@{$limit_array[$arrayp]}, $reg);
614 if ($has_limit == 1) {
615 push(@obst_limit_func, "static const unsigned limit_reg_${op}_".($in ? "in" : "out")."_". ${idx} ."[] = { ");
617 my $limitbitsetlen = $regclass2len{$class};
618 my $limitarraylen = $limitbitsetlen / 32 + ($limitbitsetlen % 32 > 0 ? 1 : 0);
619 for(my $i = 0; $i < $limitarraylen; $i++) {
620 my $limitarraypart = $limit_array[$i];
624 push(@obst_limit_func, ", ");
628 $temp = "0xFFFFFFFF";
630 foreach my $reg (@{$limitarraypart}) {
633 } elsif(defined($temp)) {
636 $temp .= "BIT(REG_".uc(${reg}).")";
639 push(@obst_limit_func, "${temp}");
641 push(@obst_limit_func, "0");
644 push(@obst_limit_func, " };\n");
647 return ($class, $has_limit, $same_pos, $different_pos);
651 # Gets the variable name for the execution unit assigned to this register.
653 sub get_execunit_variable_name {
656 my $uc_arch = uc($arch);
660 SRCH: foreach my $cur_type (keys(%cpu)) {
661 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
662 if ($unit eq $cur_unit) {
663 my $tp_name = "$arch\_execution_units_$cur_type";
664 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
665 $name = "&".$tp_name."[".$unit_name."]";
673 print STDERR "Invalid execution unit $unit specified!\n";