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];
25 unless ($return = do $specfile) {
26 die "couldn't parse $specfile: $@" if $@;
27 die "couldn't do $specfile: $!" unless defined $return;
28 die "couldn't run $specfile" unless $return;
32 my $target_c = $target_dir."/gen_".$arch."_regalloc_if.c";
33 my $target_h = $target_dir."/gen_".$arch."_regalloc_if.h";
34 my $target_h_t = $target_dir."/gen_".$arch."_regalloc_if_t.h";
37 sub translate_reg_type {
41 return "arch_register_type_none";
47 push(@types, "arch_register_type_caller_save");
51 push(@types, "arch_register_type_callee_save");
55 push(@types, "arch_register_type_ignore");
59 push(@types, "arch_register_type_joker");
63 push(@types, "arch_register_type_virtual");
67 push(@types, "arch_register_type_state");
70 return join(" | ", @types);
75 my @obst_regtypes_def; # stack for the register type variables definitions
76 my @obst_regtypes_decl;# stack for the register type variables declarations
77 my @obst_regclasses; # stack for the register class variables
78 my @obst_classdef; # stack to define a name for a class index
79 my @obst_regdef; # stack to define a name for a register index
80 my @obst_reginit; # stack for the register type inits
81 my @obst_req; # stack for the register requirements
82 my @obst_limit_func; # stack for functions to return a subset of a register class
83 my @obst_header_all; # stack for some extern struct defs needed for bearch_$arch include
95 push(@obst_classdef, "enum reg_classes {\n");
99 # assure, the initialization is done only once
100 push(@obst_reginit, "\tstatic int run_once = 0;\n");
101 push(@obst_reginit, "\n");
102 push(@obst_reginit, "\tif (run_once)\n");
103 push(@obst_reginit, "\t\treturn;\n");
104 push(@obst_reginit, "\trun_once = 1;\n");
106 # generate register type and class variable, init function and default requirements
107 foreach my $class_name (keys(%reg_classes)) {
108 my @class = @{ $reg_classes{"$class_name"} };
109 my $old_classname = $class_name;
111 $class_name = $arch."_".$class_name;
112 $numregs = "N_".$class_name."_REGS";
113 $class_ptr = "&".$arch."_reg_classes[CLASS_".$class_name."]";
114 $class_mode = pop(@class)->{"mode"};
116 push(@obst_regtypes_decl, "extern arch_register_t ".$class_name."_regs[$numregs];\n");
117 push(@obst_regtypes_def, "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/* Init of all registers in class '$class_name' */\n\n");
124 push(@obst_reginit, "\t/* set largest possible mode for '$class_name' */\n");
125 push(@obst_reginit, "\t$arch\_reg_classes[CLASS_".$class_name."].mode = $class_mode;\n\n");
126 push(@obst_regdef, "enum reg_".$class_name."_values {\n");
128 # realname is name if not set by user
129 $_->{"realname"} = $_->{"name"} if (! exists($_->{"realname"}));
131 $reg2class{$_->{"name"}} = { "class" => $old_classname, "index" => $idx }; # remember reg to class for later use
132 push(@obst_regdef, "\tREG_".uc($_->{"name"})." = $idx,\n");
133 push(@obst_reginit, "\t${class_name}_regs[$idx].name = \"".$_->{"realname"}."\";\n");
134 push(@obst_reginit, "\t${class_name}_regs[$idx].reg_class = $class_ptr;\n");
135 push(@obst_reginit, "\t${class_name}_regs[$idx].index = $idx;\n");
136 push(@obst_reginit, "\t${class_name}_regs[$idx].type = ".translate_reg_type($_->{"type"}).";\n");
137 push(@obst_reginit, "\t${class_name}_regs[$idx].data = ".get_execunit_variable_name($_->{"unit"}).";\n");
138 push(@obst_reginit, "\n");
141 $regclass2len{$old_classname} = $idx;
142 push(@obst_regdef, "\t$numregs = $idx\n");
143 push(@obst_regdef, "};\n\n");
148 push(@obst_classdef, "\tN_CLASSES = ".scalar(keys(%reg_classes))."\n");
149 push(@obst_classdef, "};\n\n");
151 # generate node-register constraints
152 foreach my $op (keys(%nodes)) {
153 my %n = %{ $nodes{"$op"} };
155 next if (!exists($n{"reg_req"}));
159 push(@obst_req, "/* IN requirements for '$op' */\n");
160 # check for argument requirements
161 if (exists($n{"reg_req"}{"in"})) {
162 generate_requirements(\%n, $op, "in");
165 push(@obst_req, "/* OUT requirements for '$op' */\n");
166 # check for result requirements
167 if (exists($n{"reg_req"}{"out"})) {
168 generate_requirements(\%n, $op, "out");
174 # generate header _t (internal usage) file
175 open(OUT, ">$target_h_t") || die("Could not open $target_h_t, reason: $!\n");
177 my $creation_time = localtime(time());
183 * Generated register classes from spec.
185 * DO NOT EDIT THIS FILE, your changes will be lost.
186 * Edit $specfile instead.
187 * created by: $0 $specfile $target_dir
188 * date: $creation_time
190 #ifndef _GEN_${tmp}_REGALLOC_IF_T_H_
191 #define _GEN_${tmp}_REGALLOC_IF_T_H_
193 #include "gen_${arch}_regalloc_if.h"
197 print OUT @obst_header_t;
199 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_T_H_ */\n";
203 # generate header (external usage) file
204 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
206 $creation_time = localtime(time());
210 * Contains additional external requirements defs for external includes.
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_H_
218 #define _GEN_${tmp}_REGALLOC_IF_H_
220 #include "../bearch.h"
221 #include "${arch}_nodes_attr.h"
225 print OUT @obst_regdef, "\n";
227 print OUT @obst_classdef, "\n";
229 print OUT @obst_regtypes_decl, "\n";
231 print OUT "extern arch_register_class_t $arch\_reg_classes[N_CLASSES];\n\n";
233 print OUT "void ".$arch."_register_init(void *isa_ptr);\n\n";
235 print OUT @obst_header_all, "\n";
237 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_H_ */\n";
244 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
246 $creation_time = localtime(time());
250 * The generated interface for the register allocator.
251 * Contains register classes and types and register constraints
252 * for all nodes where constraints were given in spec.
254 * DO NOT EDIT THIS FILE, your changes will be lost.
255 * Edit $specfile instead.
256 * created by: $0 $specfile $target_dir
257 * date: $creation_time
263 #include "gen_${arch}_regalloc_if.h"
264 #include "gen_${arch}_machine.h"
265 #include "bearch_${arch}_t.h"
266 #include "${arch}_map_regs.h"
272 #define BIT(x) (1 << (x % 32))
276 print OUT "arch_register_class_t ${arch}_reg_classes[] = {\n\t".join(",\n\t", @obst_regclasses)."\n};\n\n";
278 print OUT @obst_regtypes_def, "\n";
280 print OUT "void ${arch}_register_init(void *isa_ptr) {\n";
281 print OUT @obst_reginit;
284 print OUT @obst_limit_func;
291 # Remember the register class for each index in the given requirements.
292 # We need this information for requirements like "in_sX" or "out_dX"
293 # @return array of classes corresponding to the requirement for each index
295 sub build_inout_idx_class {
301 if (exists($n->{"reg_req"}{"$inout"})) {
302 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
304 for (my $idx = 0; $idx <= $#reqs; $idx++) {
307 if ($reqs[$idx] eq "none") {
309 } elsif (is_reg_class($reqs[$idx])) {
310 $class = $reqs[$idx];
312 my @regs = split(/ /, $reqs[$idx]);
313 GET_CLASS: foreach my $reg (@regs) {
314 if ($reg =~ /!?(in|out)\_r\d+/ || $reg =~ /!in/) {
315 $class = "UNKNOWN_CLASS";
318 $class = get_reg_class($reg);
319 if (!defined $class) {
320 die("Could not get ".uc($inout)." register class for '$op' pos $idx (reg $reg) ... exiting.\n");
329 push(@idx_class, $class);
337 # Generates the requirements for the given description
339 sub generate_requirements {
344 # get classes for the complementary direction
345 my $outin = ($inout eq "in") ? "out" : "in";
347 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
349 for (my $idx = 0; $idx <= $#reqs; $idx++) {
352 my $tmp2 = "const arch_register_req_t ${op}_reg_req_${inout}_${idx} = ";
354 if ($reqs[$idx] eq "none") {
356 $tmp2 .= "\tarch_register_req_type_none,\n";
357 $tmp2 .= "\tNULL, /* regclass */\n";
358 $tmp2 .= "\tNULL, /* limit bitset */\n";
359 $tmp2 .= "\t-1, /* same pos */\n";
360 $tmp2 .= "\t-1 /* different pos */\n";
363 push(@obst_req, $tmp2."\n");
364 push(@obst_header_t, "extern const arch_register_req_t ${op}_reg_req_${inout}_${idx};\n");
365 } elsif ($reqs[$idx] =~ /^new_reg_(.*)$/) {
366 if (is_reg_class($1)) {
368 $tmp2 .= "\tarch_register_req_type_should_be_different_from_all,\n";
369 $tmp2 .= "\t&${arch}_reg_classes[CLASS_${arch}_$1],\n";
370 $tmp2 .= "\tNULL, /* limit bitset */\n";
371 $tmp2 .= "\t-1, /* same pos */\n";
372 $tmp2 .= "\t-1 /* different pos */\n";
375 push(@obst_req, $tmp2."\n");
376 push(@obst_header_t, "extern const arch_register_req_t ${op}_reg_req_${inout}_${idx};\n");
378 print STDERR "Invalid register class '$1' given in OUT requirement $idx for '$op'.\n";
380 } elsif (is_reg_class($reqs[$idx])) {
381 my $class = $reqs[$idx];
383 $tmp2 .= "\tarch_register_req_type_normal,\n";
384 $tmp2 .= "\t&${arch}_reg_classes[CLASS_${arch}_${class}],\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");
394 my ($class, $has_limit, $same_pos, $different_pos) = build_subset_class_func($n, $op, $idx, (($inout eq "in") ? 1 : 0), $reqs[$idx]);
396 if (!defined($class)) {
397 die("Could not build subset for ".uc($inout)." requirements '$op' pos $idx ... exiting.\n");
401 push(@req_type_mask, "arch_register_req_type_limited");
403 if (defined($same_pos)) {
404 push(@req_type_mask, "arch_register_req_type_should_be_same");
406 if (defined($different_pos)) {
407 if ($different_pos == 666) {
408 push(@req_type_mask, "arch_register_req_type_should_be_different_from_all");
409 undef $different_pos;
411 push(@req_type_mask, "arch_register_req_type_should_be_different");
416 $tmp2 .= "\t".join(" | ", @req_type_mask).",\n";
417 $tmp2 .= "\t&${arch}_reg_classes[CLASS_${arch}_${class}],\n";
418 $tmp2 .= "\t".($has_limit ? "limit_reg_${op}_${inout}_${idx}" : "NULL").",\n";
419 $tmp2 .= "\t".(defined($same_pos) ? $same_pos : "-1").",\n";
420 $tmp2 .= "\t".(defined($different_pos) ? $different_pos : "-1")."\n";
423 push(@obst_req, $tmp2."\n");
424 push(@obst_header_t, "extern const arch_register_req_t ${op}_reg_req_${inout}_${idx};\n");
431 # Determines whether $name is a specified register class or not.
432 # @return 1 if name is register class, 0 otherwise
436 return 1 if exists($reg_classes{"$name"});
441 # Returns the register class for a given register.
442 # @return class or undef
446 $reg = substr($reg, 1) if ($reg =~ /!.*/);
447 return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
452 # Returns the index of a given register within it's register class.
453 # @return index or undef
457 return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
462 # Generates the function for a given $op and a given IN-index
463 # which returns a subset of possible register from a register class
464 # @return classname from which the subset is derived or undef and
465 # pos which corresponds to in/out reference position or undef
467 sub build_subset_class_func {
471 my $same_pos = undef;
472 my $different_pos = undef;
479 # build function header
485 my $outin = $in ? "out" : "in";
486 my @regs = split(/ /, shift);
488 my @idx_class = build_inout_idx_class($n, $op, $outin);
490 # set/unset registers
491 CHECK_REQS: foreach (@regs) {
492 if (/(!)?$outin\_r(\d+)/) {
493 if (($1 && defined($different_pos)) || (!$1 && defined($same_pos))) {
494 print STDERR "Multiple in/out references of same type in one requirement not allowed.\n";
495 return (undef, undef, undef, undef);
499 $different_pos = $in ? -$2 : $2 - 1;
502 $same_pos = $in ? -$2 : $2 - 1;
505 $class = $idx_class[$2 - 1];
509 $class = $idx_class[0];
510 return ($class, 0, undef, 666);
514 if (substr($_, 0, 1) eq "!") {
515 if (defined($neg) && $neg == 0) {
516 # we have seen a positiv constraint as first one but this one is negative
517 # this doesn't make sense
518 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
519 return (undef, undef, undef, undef);
522 if (!defined($neg)) {
526 $_ = substr($_, 1); # skip '!'
529 if (defined($neg) && $neg == 1) {
530 # we have seen a negative constraint as first one but this one is positive
531 # this doesn't make sense
532 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
533 return (undef, undef, undef, undef);
540 # check if register belongs to one of the given classes
541 $temp = get_reg_class($_);
542 if (!defined($temp)) {
543 print STDERR "Unknown register '$_'!\n";
544 return (undef, undef, undef, undef);
548 if (!defined($class)) {
550 } elsif ($class ne $temp) {
551 # all registers must belong to the same class
552 print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
553 return (undef, undef, undef, undef);
556 # calculate position inside the initializer bitfield (only 32 bits per
558 my $regidx = get_reg_index($_);
559 my $arrayp = $regidx / 32;
560 push(@{$limit_array[$arrayp]}, $_);
563 # don't allow ignore regs in negative constraints
565 my @cur_class = @{ $reg_classes{"$class"} };
566 for (my $idx = 0; $idx <= $#cur_class; $idx++) {
567 if (defined($cur_class[$idx]{"type"}) && ($cur_class[$idx]{"type"} & 4)) {
568 my $reg = $cur_class[$idx]{"name"};
569 my $regix = get_reg_index($reg);
570 my $arrayp = $regix / 32;
571 push(@{$limit_array[$arrayp]}, $reg);
576 if ($has_limit == 1) {
577 push(@obst_limit_func, "static const unsigned limit_reg_${op}_".($in ? "in" : "out")."_". ${idx} ."[] = { ");
579 my $limitbitsetlen = $regclass2len{$class};
580 my $limitarraylen = $limitbitsetlen / 32 + ($limitbitsetlen % 32 > 0 ? 1 : 0);
581 for(my $i = 0; $i < $limitarraylen; $i++) {
582 my $limitarraypart = $limit_array[$i];
586 push(@obst_limit_func, ", ");
590 $temp = "0xFFFFFFFF";
592 foreach my $reg (@{$limitarraypart}) {
595 } elsif(defined($temp)) {
598 $temp .= "BIT(REG_".uc(${reg}).")";
601 push(@obst_limit_func, "${temp}");
603 push(@obst_limit_func, "0");
606 push(@obst_limit_func, " };\n");
609 return ($class, $has_limit, $same_pos, $different_pos);
613 # Gets the variable name for the execution unit assigned to this register.
615 sub get_execunit_variable_name {
618 my $uc_arch = uc($arch);
622 SRCH: foreach my $cur_type (keys(%cpu)) {
623 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
624 if ($unit eq $cur_unit) {
625 my $tp_name = "$arch\_execution_units_$cur_type";
626 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
627 $name = "&".$tp_name."[".$unit_name."]";
635 print STDERR "Invalid execution unit $unit specified!\n";