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 warn "couldn't parse $specfile: $@" if $@;
27 warn "couldn't do $specfile: $!" unless defined $return;
28 warn "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");
66 return join(" | ", @types);
71 my @obst_regtypes_def; # stack for the register type variables definitions
72 my @obst_regtypes_decl;# stack for the register type variables declarations
73 my @obst_regclasses; # stack for the register class variables
74 my @obst_classdef; # stack to define a name for a class index
75 my @obst_regdef; # stack to define a name for a register index
76 my @obst_reginit; # stack for the register type inits
77 my @obst_req; # stack for the register requirements
78 my @obst_limit_func; # stack for functions to return a subset of a register class
79 my @obst_header_all; # stack for some extern struct defs needed for bearch_$arch include
91 push(@obst_classdef, "enum reg_classes {\n");
95 # assure, the initialization is done only once
96 push(@obst_reginit, "\tstatic int run_once = 0;\n");
97 push(@obst_reginit, "\n");
98 push(@obst_reginit, "\tif (run_once)\n");
99 push(@obst_reginit, "\t\treturn;\n");
100 push(@obst_reginit, "\trun_once = 1;\n");
102 # generate register type and class variable, init function and default requirements
103 foreach my $class_name (keys(%reg_classes)) {
104 my @class = @{ $reg_classes{"$class_name"} };
105 my $old_classname = $class_name;
107 $class_name = $arch."_".$class_name;
108 $numregs = "N_".$class_name."_REGS";
109 $class_ptr = "&".$arch."_reg_classes[CLASS_".$class_name."]";
110 $class_mode = pop(@class)->{"mode"};
112 push(@obst_regtypes_decl, "extern arch_register_t ".$class_name."_regs[$numregs];\n");
113 push(@obst_regtypes_def, "arch_register_t ".$class_name."_regs[$numregs];\n");
115 push(@obst_classdef, "\tCLASS_$class_name = $class_idx,\n");
116 push(@obst_regclasses, "{ \"$class_name\", $numregs, NULL, ".$class_name."_regs }");
119 push(@obst_reginit, "\t/* Init of all registers in class '$class_name' */\n\n");
120 push(@obst_reginit, "\t/* set largest possible mode for '$class_name' */\n");
121 push(@obst_reginit, "\t$arch\_reg_classes[CLASS_".$class_name."].mode = $class_mode;\n\n");
122 push(@obst_regdef, "enum reg_".$class_name."_values {\n");
124 # realname is name if not set by user
125 $_->{"realname"} = $_->{"name"} if (! exists($_->{"realname"}));
127 $reg2class{$_->{"name"}} = { "class" => $old_classname, "index" => $idx }; # remember reg to class for later use
128 push(@obst_regdef, "\tREG_".uc($_->{"name"})." = $idx,\n");
129 push(@obst_reginit, "\t${class_name}_regs[$idx].name = \"".$_->{"realname"}."\";\n");
130 push(@obst_reginit, "\t${class_name}_regs[$idx].reg_class = $class_ptr;\n");
131 push(@obst_reginit, "\t${class_name}_regs[$idx].index = $idx;\n");
132 push(@obst_reginit, "\t${class_name}_regs[$idx].type = ".translate_reg_type($_->{"type"}).";\n");
133 push(@obst_reginit, "\t${class_name}_regs[$idx].data = ".get_execunit_variable_name($_->{"unit"}).";\n");
134 push(@obst_reginit, "\n");
137 $regclass2len{$old_classname} = $idx;
138 push(@obst_regdef, "\t$numregs = $idx\n");
139 push(@obst_regdef, "};\n\n");
144 push(@obst_classdef, "\tN_CLASSES = ".scalar(keys(%reg_classes))."\n");
145 push(@obst_classdef, "};\n\n");
147 # generate node-register constraints
148 foreach my $op (keys(%nodes)) {
149 my %n = %{ $nodes{"$op"} };
151 next if (!exists($n{"reg_req"}));
155 push(@obst_req, "/* IN requirements for '$op' */\n");
156 # check for argument requirements
157 if (exists($n{"reg_req"}{"in"})) {
158 generate_requirements(\%n, $op, "in");
161 push(@obst_req, "/* OUT requirements for '$op' */\n");
162 # check for result requirements
163 if (exists($n{"reg_req"}{"out"})) {
164 generate_requirements(\%n, $op, "out");
170 # generate header _t (internal usage) file
171 open(OUT, ">$target_h_t") || die("Could not open $target_h_t, reason: $!\n");
173 my $creation_time = localtime(time());
179 * Generated register classes from spec.
181 * DO NOT EDIT THIS FILE, your changes will be lost.
182 * Edit $specfile instead.
183 * created by: $0 $specfile $target_dir
184 * date: $creation_time
186 #ifndef _GEN_${tmp}_REGALLOC_IF_T_H_
187 #define _GEN_${tmp}_REGALLOC_IF_T_H_
189 #include "gen_${arch}_regalloc_if.h"
193 print OUT @obst_header_t;
195 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_T_H_ */\n";
199 # generate header (external usage) file
200 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
202 $creation_time = localtime(time());
206 * Contains additional external requirements defs for external includes.
208 * DO NOT EDIT THIS FILE, your changes will be lost.
209 * Edit $specfile instead.
210 * created by: $0 $specfile $target_dir
211 * date: $creation_time
213 #ifndef _GEN_${tmp}_REGALLOC_IF_H_
214 #define _GEN_${tmp}_REGALLOC_IF_H_
216 #include "../bearch.h"
217 #include "${arch}_nodes_attr.h"
221 print OUT @obst_regdef, "\n";
223 print OUT @obst_classdef, "\n";
225 print OUT @obst_regtypes_decl, "\n";
227 print OUT "extern arch_register_class_t $arch\_reg_classes[N_CLASSES];\n\n";
229 print OUT "void ".$arch."_register_init(void *isa_ptr);\n\n";
231 print OUT @obst_header_all, "\n";
233 print OUT "\n#endif /* _GEN_$tmp\_REGALLOC_IF_H_ */\n";
240 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
242 $creation_time = localtime(time());
246 * The generated interface for the register allocator.
247 * Contains register classes and types and register constraints
248 * for all nodes where constraints were given in spec.
250 * 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
259 #include "gen_${arch}_regalloc_if.h"
260 #include "gen_${arch}_machine.h"
261 #include "bearch_${arch}_t.h"
262 #include "${arch}_map_regs.h"
268 #define BIT(x) (1 << (x % 32))
272 print OUT "arch_register_class_t ${arch}_reg_classes[] = {\n\t".join(",\n\t", @obst_regclasses)."\n};\n\n";
274 print OUT @obst_regtypes_def, "\n";
276 print OUT "void ${arch}_register_init(void *isa_ptr) {\n";
277 print OUT @obst_reginit;
280 print OUT @obst_limit_func;
287 # Remember the register class for each index in the given requirements.
288 # We need this information for requirements like "in_sX" or "out_dX"
289 # @return array of classes corresponding to the requirement for each index
291 sub build_inout_idx_class {
297 if (exists($n->{"reg_req"}{"$inout"})) {
298 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
300 for (my $idx = 0; $idx <= $#reqs; $idx++) {
303 if ($reqs[$idx] eq "none") {
305 } elsif (is_reg_class($reqs[$idx])) {
306 $class = $reqs[$idx];
308 my @regs = split(/ /, $reqs[$idx]);
309 GET_CLASS: foreach my $reg (@regs) {
310 if ($reg =~ /!?(in|out)\_r\d+/ || $reg =~ /!in/) {
311 $class = "UNKNOWN_CLASS";
314 $class = get_reg_class($reg);
315 if (!defined $class) {
316 die("Could not get ".uc($inout)." register class for '$op' pos $idx (reg $reg) ... exiting.\n");
325 push(@idx_class, $class);
333 # Generates the requirements for the given description
335 sub generate_requirements {
340 # get classes for the complementary direction
341 my $outin = ($inout eq "in") ? "out" : "in";
343 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
345 for (my $idx = 0; $idx <= $#reqs; $idx++) {
348 my $tmp2 = "const arch_register_req_t ${op}_reg_req_${inout}_${idx} = ";
350 if ($reqs[$idx] eq "none") {
352 $tmp2 .= "\tarch_register_req_type_none,\n";
353 $tmp2 .= "\tNULL, /* regclass */\n";
354 $tmp2 .= "\tNULL, /* limit bitset */\n";
355 $tmp2 .= "\t-1, /* same pos */\n";
356 $tmp2 .= "\t-1 /* different pos */\n";
359 push(@obst_req, $tmp2."\n");
360 push(@obst_header_t, "extern const arch_register_req_t ${op}_reg_req_${inout}_${idx};\n");
361 } elsif ($reqs[$idx] =~ /^new_reg_(.*)$/) {
362 if (is_reg_class($1)) {
364 $tmp2 .= "\tarch_register_req_type_should_be_different_from_all,\n";
365 $tmp2 .= "\t&${arch}_reg_classes[CLASS_${arch}_$1],\n";
366 $tmp2 .= "\tNULL, /* limit bitset */\n";
367 $tmp2 .= "\t-1, /* same pos */\n";
368 $tmp2 .= "\t-1 /* different pos */\n";
371 push(@obst_req, $tmp2."\n");
372 push(@obst_header_t, "extern const arch_register_req_t ${op}_reg_req_${inout}_${idx};\n");
374 print STDERR "Invalid register class '$1' given in OUT requirement $idx for '$op'.\n";
376 } elsif (is_reg_class($reqs[$idx])) {
377 my $class = $reqs[$idx];
379 $tmp2 .= "\tarch_register_req_type_normal,\n";
380 $tmp2 .= "\t&${arch}_reg_classes[CLASS_${arch}_${class}],\n";
381 $tmp2 .= "\tNULL, /* limit bitset */\n";
382 $tmp2 .= "\t-1, /* same pos */\n";
383 $tmp2 .= "\t-1 /* different pos */\n";
386 push(@obst_req, $tmp2."\n");
387 push(@obst_header_t, "extern const arch_register_req_t ${op}_reg_req_${inout}_${idx};\n");
390 my ($class, $has_limit, $same_pos, $different_pos) = build_subset_class_func($n, $op, $idx, (($inout eq "in") ? 1 : 0), $reqs[$idx]);
392 if (!defined($class)) {
393 die("Could not build subset for ".uc($inout)." requirements '$op' pos $idx ... exiting.\n");
397 push(@req_type_mask, "arch_register_req_type_limited");
399 if (defined($same_pos)) {
400 push(@req_type_mask, "arch_register_req_type_should_be_same");
402 if (defined($different_pos)) {
403 if ($different_pos == 666) {
404 push(@req_type_mask, "arch_register_req_type_should_be_different_from_all");
405 undef $different_pos;
407 push(@req_type_mask, "arch_register_req_type_should_be_different");
412 $tmp2 .= "\t".join(" | ", @req_type_mask).",\n";
413 $tmp2 .= "\t&${arch}_reg_classes[CLASS_${arch}_${class}],\n";
414 $tmp2 .= "\t".($has_limit ? "limit_reg_${op}_${inout}_${idx}" : "NULL").",\n";
415 $tmp2 .= "\t".(defined($same_pos) ? $same_pos : "-1").",\n";
416 $tmp2 .= "\t".(defined($different_pos) ? $different_pos : "-1")."\n";
419 push(@obst_req, $tmp2."\n");
420 push(@obst_header_t, "extern const arch_register_req_t ${op}_reg_req_${inout}_${idx};\n");
427 # Determines whether $name is a specified register class or not.
428 # @return 1 if name is register class, 0 otherwise
432 return 1 if exists($reg_classes{"$name"});
437 # Returns the register class for a given register.
438 # @return class or undef
442 $reg = substr($reg, 1) if ($reg =~ /!.*/);
443 return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
448 # Returns the index of a given register within it's register class.
449 # @return index or undef
453 return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
458 # Generates the function for a given $op and a given IN-index
459 # which returns a subset of possible register from a register class
460 # @return classname from which the subset is derived or undef and
461 # pos which corresponds to in/out reference position or undef
463 sub build_subset_class_func {
467 my $same_pos = undef;
468 my $different_pos = undef;
475 # build function header
481 my $outin = $in ? "out" : "in";
482 my @regs = split(/ /, shift);
484 my @idx_class = build_inout_idx_class($n, $op, $outin);
486 # set/unset registers
487 CHECK_REQS: foreach (@regs) {
488 if (/(!)?$outin\_r(\d+)/) {
489 if (($1 && defined($different_pos)) || (!$1 && defined($same_pos))) {
490 print STDERR "Multiple in/out references of same type in one requirement not allowed.\n";
491 return (undef, undef, undef, undef);
495 $different_pos = $in ? -$2 : $2 - 1;
498 $same_pos = $in ? -$2 : $2 - 1;
501 $class = $idx_class[$2 - 1];
505 $class = $idx_class[0];
506 return ($class, 0, undef, 666);
510 if (substr($_, 0, 1) eq "!") {
511 if (defined($neg) && $neg == 0) {
512 # we have seen a positiv constraint as first one but this one is negative
513 # this doesn't make sense
514 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
515 return (undef, undef, undef, undef);
518 if (!defined($neg)) {
522 $_ = substr($_, 1); # skip '!'
525 if (defined($neg) && $neg == 1) {
526 # we have seen a negative constraint as first one but this one is positive
527 # this doesn't make sense
528 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
529 return (undef, undef, undef, undef);
536 # check if register belongs to one of the given classes
537 $temp = get_reg_class($_);
538 if (!defined($temp)) {
539 print STDERR "Unknown register '$_'!\n";
540 return (undef, undef, undef, undef);
544 if (!defined($class)) {
546 } elsif ($class ne $temp) {
547 # all registers must belong to the same class
548 print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
549 return (undef, undef, undef, undef);
552 # calculate position inside the initializer bitfield (only 32 bits per
554 my $regidx = get_reg_index($_);
555 my $arrayp = $regidx / 32;
556 push(@{$limit_array[$arrayp]}, $_);
559 # don't allow ignore regs in negative constraints
561 my @cur_class = @{ $reg_classes{"$class"} };
562 for (my $idx = 0; $idx <= $#cur_class; $idx++) {
563 if (defined($cur_class[$idx]{"type"}) && ($cur_class[$idx]{"type"} & 4)) {
564 my $reg = $cur_class[$idx]{"name"};
565 my $regix = get_reg_index($reg);
566 my $arrayp = $regix / 32;
567 push(@{$limit_array[$arrayp]}, $reg);
572 if ($has_limit == 1) {
573 push(@obst_limit_func, "static const unsigned limit_reg_${op}_".($in ? "in" : "out")."_${idx}[] = { ");
575 my $limitbitsetlen = $regclass2len{$class};
576 my $limitarraylen = $limitbitsetlen / 32 + ($limitbitsetlen % 32 > 0 ? 1 : 0);
577 for(my $i = 0; $i < $limitarraylen; $i++) {
578 my $limitarraypart = $limit_array[$i];
582 push(@obst_limit_func, ", ");
586 $temp = "0xFFFFFFFF";
588 foreach my $reg (@{$limitarraypart}) {
591 } elsif(defined($temp)) {
594 $temp .= "BIT(REG_".uc(${reg}).")";
597 push(@obst_limit_func, "${temp}");
599 push(@obst_limit_func, "0");
602 push(@obst_limit_func, " };\n");
605 return ($class, $has_limit, $same_pos, $different_pos);
609 # Gets the variable name for the execution unit assigned to this register.
611 sub get_execunit_variable_name {
614 my $uc_arch = uc($arch);
618 SRCH: foreach my $cur_type (keys(%cpu)) {
619 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
620 if ($unit eq $cur_unit) {
621 my $tp_name = "$arch\_execution_units_$cur_type";
622 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
623 $name = "&".$tp_name."[".$unit_name."]";
631 print STDERR "Invalid execution unit $unit specified!\n";